Extracting data from array in Yii 2

I'm trying to get data from DB and show it in menu.

echo NavX::widget([ 'options' => ['class' => 'navbar-nav navbar-right'], 'items' => [ array_map(function ($model) { return [ 'label' => $model['param2'], 'url' => ["/users/{$model['param3']}"], ]; }, Model::find()->where(['userID' => 32])->asArray()->all()), ], ]);

But unfortunately this code is wrong, error says:

Array to string conversion

Is there any other way to fix it ?

-------------Problems Reply------------

My guess, without testing:

echo NavX::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => [
array_map(function ($model) {
return [
'label' => $model['param2'],
'url' => "/users/{$model['param3']}", // <--- string, not array!
}, Model::find()->where(['userID' => 32])->asArray()->all()),

One of the "Yii" way to do it will be using built-in ArrayHelper:

use yii\helpers\ArrayHelper;


$models = Model::find()->where(['userID' => 32])->asArray()->all();
$items = ArrayHelper::toArray($models, [
'app\models\Model' => [
'label' => function ($model) {
// Add label generation code here
'url' => function ($model) {
// Add url generation code here

Then just pass the $items to your view with menu.

Official docs:

  • ArrayHelper::toArray()
Category:php Views:3 Time:2018-02-03
Tags: php yii yii2

Related post

  • Extract data from array in DB (rails) 2011-01-21

    I am trying to extract some data from an array with the following syntax: @entries_from_db = XrEntry.find(:all, :conditions => [:FeedURI => uri ], :select => 'json') The :FeedURI is the record that contains an array with uri's ["123456", "23

  • Extract data from 2d array java 2011-04-10

    How can i extract data from a 2d array.. e and b are indexes of the array int e = IO.readInt(); int b = IO.readInt(); int a[][] = { { 8, 2, 6, 5 }, // row 0 { 6, 3, 1, 0 }, // row 1 { 8, 7, 9, 6 } }; for (int i = 0; i < a.length; i++) { for (int j

  • Programmatically extract data from an Excel spreadsheet 2009-08-07

    Is there a simple way, using some common Unix scripting language (Perl/Python/Ruby) or command line utility, to convert an Excel Spreadsheet file to CSV? Specifically, this one: http://www.econ.yale.edu/~shiller/data/ie_data.xls And specifically the

  • Extract data from website via PHP 2010-01-07

    I am trying to create a simple alert app for some friends. Basically i want to be able to extract data "price" and "stock availability" from a webpage like the folowing two: http://www.sparkfun.com/commerce/product_info.php?products_id=5 http://www.s

  • Getting data from array of DataSet objects returned from web service 2010-04-12

    I have a web service that I want to access when it is added as a web reference to my C# project. A particular method in the web service takes a SQL query string and returns the results of the query as a custom type. When I add the web service referen

  • How can I extract data from a .fig file in MATLAB? 2010-06-04

    I know this is really basic, but I am new to MATLAB. After opening a .fig file, how do you actually work with the plotted data in the command window? All I see is the plot. I'm not sure how to actually get the data. --------------Solutions-----------

  • Extract dates from filename 2010-06-15

    I have a situation where I need to extract dates from the file names whose general pattern is [filename_]YYYYMMDD[.fileExtension] e.g. "xxx_20100326.xls" or x2v_20100326.csv The below program does the work //Number of charecter in the substring is se

  • Extracting data from excel using interop - ignore certain cells where no data is contained 2010-10-27

    I'm using c# and visual studio to extract data from an excel sheet, put it into an array (adding commas to the end of each string) and then output it to a .CSV file. Now I've run into a snag that occurs when a cell has either no data, or not the data

  • How to extract data from a NSArray fill with NSDictionary? 2010-11-23

    i try to make a kind of "sql distinct" request with core data, so i set up my NSFetchrequest like this NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; [fetchRequest setEntity:[NSEntityDescription entityForName:@"my_table" inManagedObjec

  • Nokogiri extract data from xml 2011-03-04

    i try to extract data from a xml in rails application with the Nokogiri gem, the xml : <item> <description> <![CDATA[<img src="something" title="anothething"> <p>text, bla bla...</p>]]> </description> </it

  • Custom class for extracting data from the formcollection in a postback in ASP.net MVC 2011-03-11

    I've been wondering about the possibility of generating a strongly-type object from the formcollection to extract data from it. In other words, is it possible to generate a class depending on the keys in the formcollection object? For example: Say yo

  • Extracting Data From Remote XML & Creating Wordpress Post 2011-03-15

    I've been searching for some time to figure out how to extract data from a remote XML file and then create a post automatically with the parsed XML data. I have figured out the functions to create a post using cURL/PHP but I'm not sure how to pull da

  • matplotlib - extracting data from contour lines 2011-04-14

    I would like to get data from a single contour of evenly spaced 2D data (an image-like data). Based on the example found in a similar question: How can I get the (x,y) values of the line that is ploted by a contour plot (matplotlib)? >>> imp

  • I have two files xml, and I need to extract data from first xml file and insert in to second xml file 2011-07-19

    I have two files xml, and I need to extract data from first xml file and insert in to second xml file. first file: <?xml version="1.0" encoding="utf-8"?> <ITEMS> <EVENT> <codpal>8387</codpal> <programming>ProgramNa

  • How to condense/simplify this block of code that extracts data from a csv file? 2011-11-16

    I have this code which extracts data from a csv file and then reformats it so that it can be compared with another data set: def dataExtract dates = File.open(@filename_data).read.scan /\d{2}\/\d{2}\/\d{2}/ data_extracted = [] index = 0 dates.each do

  • Asp.Net Extracting data from deep within a dictionary- 2011-12-09

    Relatively newbie here with a little question. I been extracting a json string that looks like this (in this case it is a modified return from Facebook oauth2. {"id":"555555555555555","name":"Monkey Man","last_name":"Man","first_name":"Monkey","email

  • Extract data from a sequence of bytes 2012-03-12

    Assume we have a sequence of bytes with header of '807F' which is input to PC through RS232. for example "... 80 7F 4B 97 80 7F 21 3A 80 7F ... " in which "21 3A" is our data in bold package and 21 is MSB and 3A is LSB. how can we extract data from t

  • Extracting Data from NSMuableDictionary to NSString and compering to String issue 2012-04-27

    Im Extracting Data from NSMuableDictionary to NSString and try compering to String like this: NSDictionary *error = [[NSDictionary alloc]init]; NSString *errorCode = [[NSString alloc]init]; error = [sing.globalCallsDitionary valueForKey:@"Error"]; NS

  • Extract data from an already filtered excel using C# .net interop 2012-04-28

    I am using an xls file which is "already filtered" as input for my software. So i need a way to extract only the filtered information into an object array using C# .net . But somehow, when i try to extract data from the xls, i always happen to get th

  • Another macro to extract data from multiple sheets question 2013-05-09

    Hello. Read through many of the helpful answers on macros/VBAs ... but as I've never done a macro/VBA, hopefully someone can help. I have 5 sheets in a workbook and need to extract data from all of them into 3 new sheets. Each sheet has data in Colum

  • How would you extract data from a MS Project .mpp file? 2008-09-02

    I need to extract data from a .mpp file on the network and combine it with other data from several different databases. The application can be written in Perl, VB6, VB.net or C# but must be easily scheduled from a Windows based server. What would you

  • What is the largest XML file SSIS can extract data from? 2009-03-26

    We have an architecture where we use SSIS to extract data from XML batch files into a staging database for validation, prior to exporting it into production. We control the XML format to some extent, and I've been asked to determine what the maximum

  • How to extract data from a XML file 2009-05-13

    I am using the MSXML4.0 parser in VB6. I have an XML file that is browsed and selected by the user. The XML contains lot of tags.I am intersted in mining out the data that is contained in the child nodes of a tag named leadmeasurements. The lead meas

  • How to extract data from a PDF? 2009-07-07

    My company receives data from an external company via Excel. We export this into SQL Server to run reports on the data. They are now changing to PDF format, is there a way to reliably port the data from the PDF and insert it into our SQL Server 2008

  • How do i extract data from a DataTable? 2009-08-28

    I have a DataTable that is filled in from an SQL query to a local database, but i don't know how to extract data from it. Main method (in test program): static void Main(string[] args) { string connectionString = "server=localhost\\SQLExpress;databas

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.161 (s). 11 q(s)