Remove key from multidimensional array in PHP - php

Hi I am going to make tree array for multilevel category menu and I have found the solution from below link:
stackoverflow answer
But my problem is that i am getting output of my array as FORMAT-1 and in the above given link the source array required as FORMAT-2
So can you please give me a hand for how to convert my array from FORMAT-1 to FORMAT-2
FORMAT-1
Array
(
[0] => Array
(
[category_id] => 11
[category_name] => Accessories
[parent_category_id] => 1
)
[1] => Array
(
[category_id] => 12
[category_name] => Keyrings
[parent_category_id] => 1
)
[2] => Array
(
[category_id] => 13
[category_name] => Photo Frames/Photo Albums
[parent_category_id] => 1
)
)
FORMAT-2 (I want output as below)
Array(
Array(
'category_id' => 11
'category_name' => 'Accessories'
'parent_category_id' => 1
),
Array(
'category_id' => 12
'category_name' => 'Keyrings'
'parent_category_id' => 1
),
Array(
'category_id' => 13
'category_name' => 'Photo Frames/Photo Albums'
'parent_category_id' => 1
)
);
Thanks in advance for your help and much appreciated

Both arrays are more are like same..
Format-1 looks print_r($yourarray); version
Format-2 looks like var_export($yourarray); version.
You need to have a look at the debugger functions.. print_r(), var_dump() and var_export().

If your purpose is debugging you may use <pre>var_dump()</pre>.

Related

echoing an array value giving a key from another array

I know this is simple, but cant get my head around it...
I have 2 arrays. Both populated from a database lookup.
Array 1
Array (
[sailID] => 7
[sailTag] => 100004
[assigneduser] => Jason Ellmers
[assigneddate] => 2018-05-30 17:48:57
[cutuser] => Jason Ellmers
[cutdate] => 2018-05-30 20:31:23
[stickuser] => Jason Ellmers
[stickdate] => 2018-05-30 20:38:24
[corneruser] => Jason Ellmers
[cornerdate] => 2018-05-30 20:38:54
[finishuser] => Jason Ellmers
[finishdate] => 2018-05-30 20:39:53
[checkuser] =>
[checkdate] => 0000-00-00 00:00:00
[DesignRef] => 420abcdefg
[OrderingLoft] => 1
[ClassRef] => 1
[ClothType] => Bainbridge
[ClothColour] => White
[ClothWeight] => 12oz
[SailNo] => GB342398 )
Array 2
Array (
[0] => Array (
[id] => 1
[name] => 420 )
[1] => Array (
[id] => 2
[name] => J24 ) )
What I am after doing is being able to echo to the screen $array1['Where the ClassRef is a lookup of the ID in Array2' and displays the Name from Array2]
So for the above example the Echo would be '420'
I think I could do it using a foreach or while loop but that seems a bit cumbersome???
I've had to put some test data together, but from the comment, the idea is to re-index the second array using array_column() with the id as the index, so the code (as you've worked out) is...
$array1 =[
"sailID" => 7,
"sailTag" => "100004",
"ClassRef" => 1 ];
$array2 = [["id" => 1, "name" => "420"],
["id" => 2, "name" => "J24"]];
$array2 = array_column($array2, "name", "id");
echo $array2[$array1["ClassRef"]];

Strange value set as a key in the array in cakephp2

I have the problem with the following code. The query sent was normal, just fetching all the data I need. However, When I print_r the $data, the all the datas from find('all') are not contained in [MYDATA] but also set in an unknown ['c'] and ['a'] array. Has anyone encounter this kind of weird problem ? Some examples or hint would be great. I would love to hear from you!
$data = $this->MYDATA->find(
'all',
array(
'conditions' =>
array(
array('my_id' => $var['id']),
),
'order' =>array('my_name'=>'DESC')
));
The following is the result from $data when using the 'my_name'=>'DESC' condition.
[0] => Array
(
[MYDATA] => Array
(
[id] => 79
[my_birth_day] => 1990-06-20
[my_address] => 400
[my_age] => 26
[my_name] => Joy
[my_id] => 1
[created] => 2017-06-19 15:39:44
)
[c] => Array
(
[my_test] => math
)
[a] => Array
(
[my_date] => 2017-08-13
)
)
Without using the'my_name'=>'DESC' condition. This is the correct array. I want it to have the values return like this.
[0] => Array
(
[mydata] => Array
(
[id] => 79
[my_birth_day] => 1990-06-20
[my_address] => 400
[my_age] => 26
[my_name] => Joy
[my_id] => 1
[my_test] => math
[created] => 2017-06-19 15:39:44
[my_date] => 2017-08-13
)
If you want to get most of the benefits of Cakephp then must follow cakephp 2 conventions. As you mentioned in the comment that you model is MyData then it must be MyData not MyData while fetching data from the database. Please change MYDATA to MyData.
$data = $this->MyData->find(
'all',
array(
'conditions' =>array('my_id' => $var['id']),
'order' =>array('my_name'=>'DESC')
)
);
pr($data);
You can learn more about cakephp 2 conventions from here:
https://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html

How to create an array from the existing array on basis of key and value match [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have one array I want that data belong to same day will appear on one index like in the below array I have same the date in 1 and 2 index so the result array should be like my ewault array so that data belonging to the same date will show up on same index. How can I do this?
My Array:
Array
(
[0] => Array
(
[pkMessageID] => 6
[fkPostID] => 4
[messageSenderID] => 2
[messageRecieverID] => 19
[messageBody] => Nice
[messageStatus] => 1
[DateOnly] => 2015-07-07
[messageDateAdded] => 2015-07-07 16:20:58
)
[1] => Array
(
[pkMessageID] => 5
[fkPostID] => 4
[messageSenderID] => 19
[messageRecieverID] => 2
[messageBody] => Hi I am good how r u
[messageStatus] => 1
[DateOnly] => 2015-07-06
[messageDateAdded] => 2015-07-06 16:14:05
)
[2] => Array
(
[pkMessageID] => 4
[fkPostID] => 4
[messageSenderID] => 2
[messageRecieverID] => 19
[messageBody] => Hello akhilesh how r u
[messageStatus] => 1
[DateOnly] => 2015-07-06
[messageDateAdded] => 2015-07-06 16:12:22
)
)
However, the result array should be:
Array
(
[0] => Array
(
[pkMessageID] => 6
[fkPostID] => 4
[messageSenderID] => 2
[messageRecieverID] => 19
[messageBody] => Nice
[messageStatus] => 1
[DateOnly] => 2015-07-07
[messageDateAdded] => 2015-07-07 16:20:58
)
[1] => Array
(
[0] => Array
(
[pkMessageID] => 5
[fkPostID] => 4
[messageSenderID] => 19
[messageRecieverID] => 2
[messageBody] => Hi I am good how r u
[messageStatus] => 1
[DateOnly] => 2015-07-06
[messageDateAdded] => 2015-07-06 16:14:05
)
[1] => Array
(
[pkMessageID] => 4
[fkPostID] => 4
[messageSenderID] => 2
[messageRecieverID] => 19
[messageBody] => Hello akhilesh how r u
[messageStatus] => 1
[DateOnly] => 2015-07-06
[messageDateAdded] => 2015-07-06 16:12:22
)
)
)
This can be done using the following:
$out = array();
foreach($input as $data)
{
if( ! is_array($out[$data['DateOnly']])){
$out[$data['DateOnly']] = array();
}
$out[$data['DateOnly']][] = $data;
}
I haven't the code, but i hope you get the idea. Simply use the date as an index in your array. So the result will be along the lines of.
[2015-7-6] => Array ( [0] => Array( DateOnly => 2015-7-6, ..))
[2015-7-7] => Array ( [0] => Array( DateOnly => 2015-7-7, .. ), [1] => Array(DateOnly => 2015-7-7 , .. ))

How to group these arrays accordingly?

I'm using PDO to run a mysql query with 2 joins and this is the result I get from the database:
Array
(
[0] => Array
(
[id] => 489
[cluster_id] =>
[label_value_id] => 4
[label_id] => 1
[int_value] => 40
)
[1] => Array
(
[id] => 489
[cluster_id] =>
[label_value_id] => 6
[label_id] => 2
[int_value] => 20
)
[2] => Array
(
[id] => 489
[cluster_id] =>
[label_value_id] => 9
[label_id] => 3
[int_value] => 10
)
)
The idea is that there will be multiple id's later on but for simplicities sake I only have 1 entry with id 489 for now.
I need the 3 arrays (cause of the joins) to be 1 array that looks something like this, I want a subarray based on the label-value_id => int_value relationship:
Array
(
[0] => Array
(
[id] => 489
[cluster_id] =>
[label_value_id] => 4
[label_id] => 1
[int_value] => 40
[vector_data] => Array
(
[4] => 40
[6] => 20
[9] => 10
)
)
)
I'd rather have this done in the PHP and not the Query because I dont have control over the queries when I implement this into the live application.
Any help is much appreciated!
You can use another container for gathering all batches which share the same id using its keys to transfer them. On initialization, create another container for that vector, then just push them line any normal array value:
$result = array();
foreach($array as $values) {
// initialization
if(!isset($result[$values['id']])) {
$result[$values['id']] = $values;
}
$result[$values['id']]['vector_data'][$values['label_value_id']] = $values['int_value'];
}
// $result = array_values($result); // optional key reindex
Sample Output
You can process your array from DB like this:
$pdo = array(
array(
"id" => 489,
"label_value_id" => 4,
"int_value" => 40,
),
array(
"id" => 489,
"label_value_id" => 6,
"int_value" => 20,
),
array(
"id" => 489,
"label_value_id" => 9,
"int_value" => 10,
),
);
$result = array();
foreach ($pdo as $array) {
if (!isset($result[$array['id']])) {
$result[$array['id']] = array(
"id" => $array['id'],
"vector_data" => array()
);
}
$result[$array['id']]['vector_data'][$array['label_value_id']] = $array['int_value'];
}
I have the same situation, and i solve it through PDO.
Here available examples. I reach same result with code:
$sth->fetchAll(PDO::FETCH_ASSOC|PDO::FETCH_GROUP);
think you should try it...

how can i access such a php array

i was trying to access this php array with no luck, i want to access the [icon] => icon.png
Array ( [total] => 2
[total_grouped] => 2
[notifys] => Array ( [0] => Array ( [notifytype_id] => 12
[grouped] => 930
[icon] => icon.png
[n_url] => wall_action.php?id=930
[desc] => 690706096
[text] => Array ( [0] => Sarah O'conner ) [total] => 1 )))
$arr['notifys'][0]['icon']
ETA: I'm not sure what your comment means, the following code:
$arr = array('total'=>2, 'total_grouped' => 2, 'notifys' => array(array(
'notifytype_id' => 12, 'icon' => 'icon.png')));
echo '<pre>';
print_r($arr);
echo '</pre>';
var_dump($arr['notifys'][0]['icon']);
outputs:
Array
(
[total] => 2
[total_grouped] => 2
[notifys] => Array
(
[0] => Array
(
[notifytype_id] => 12
[icon] => icon.png
)
)
)
string(8) "icon.png"
Generally, code never outputs nothing. You should be developing with all errors and notifications on.
$arr['notifys'][0]['icon']
rg = Array ( [total] => 2 [total_grouped] => 2 [notifys] => Array ( [0] => Array ( [notifytype_id] => 12 [grouped] => 930 [icon] => icon.png [n_url] => wall_action.php?id=930 [desc] => 690706096 [text] => Array ( [0] => Sarah O'conner ) [total] => 1 )));
icon = rg["notifsys"][0]["icon"];
Everybody is posting right answer. Its just you have giving a wrong deceleration of array.
Try var_dump/print_r of array and then you can easily understand nodes.
$arr = array(total => 2,
total_grouped => 2,
notifys => array( 0 => array(notifytype_id => 12,
grouped => 930,
icon => 'icon.png',
n_url => 'wall_action.php?id=930',
desc => 690706096,
text =>array(0 => 'Sarah Oconner' ),
total => 1,
),
),
);
echo $arr['notifys']['0']['icon'];

Categories