Reformating associative arrays - php

To be more specific I want to turn the following array into an associative one. The original array is indexed like [0],[1],[2],…[n]. The function I used was Set::combine of Cakephp but I couldn't recreate all three levels of the desired associative array.
Array
(
[0] => Array
(
[ACCOUNTS] => Array
(
[description] => A
)
[HEADERS] => Array
(
[description] => B
)
[COLUMNS] => Array
(
[description] => C
[id] => 8
)
)
[1] => Array
(
[ACCOUNTS] => Array
(
[description] => A1
)
[HEADERS] => Array
(
[description] => B1
)
[COLUMNS] => Array
(
[description] => C1
[id] => 9
)
)
)
The array I want to end up is the following associative array:
Array
(
[A] => Array
(
[B] => Array
(
[C] => 8
)
)
[A1] => Array
(
[B1] => Array
(
[C1] => 9
)
)
)
I can't recreate all (3) levels of the array above.

Do you mean like:
$newarray = array($first['ACCOUNTS']['description'] => array($first['HEADERS']['description'] => array($first['COLUMNS']['description'] => $first['COLUMNS']['id'])));
So if you run the following it gives what you want:
$first = array(
'ACCOUNTS' => array('description' => 'A'),
'HEADERS' => array('description' => 'B'),
'COLUMNS' => array('description' => 'C', 'id' => '8'));
echo "<pre>";
print_r($first);
$newarray = array($first['ACCOUNTS']['description'] =>
array($first['HEADERS']['description'] =>
array($first['COLUMNS']['description'] =>
$first['COLUMNS']['id'])));
print_r($newarray);
You then end up with:
Array
(
[ACCOUNTS] => Array
(
[description] => A
)
[HEADERS] => Array
(
[description] => B
)
[COLUMNS] => Array
(
[description] => C
[id] => 8
)
)
Array
(
[A] => Array
(
[B] => Array
(
[C] => 8
)
)
)

Related

merge two array result wrong array which contain second array as array rather a complete of both

I tries to array_merge in php but resultant array is not correct
1. Array ( [id] => 12 [name] => Popular )
2. Array ( [0] => Array ( [id] => 8 [name] => Flowers ) [1] => Array ( [id] => 10 [name] => Chocolates ) [2] => Array ( [id] => 11 [name] => Sweets and Dry Fruits ) )
Resultant Array
Array ( [id] => 12 [name] => Popular [0] => Array ( [id] => 8 [name] => Flowers ) [1] => Array ( [id] => 10 [name] => Chocolates ) [2] => Array ( [id] => 11 [name] => Sweets and Dry Fruits ) )
If you just want to add the new data in the same format as the existing data then use [] rather than array_merge().
$array1 = array( 'id' => 12, 'name' => 'Popular');
$array2 = array(array( 'id' => 8, 'name' => 'Flowers'),
array( 'id' => 10, 'name' => 'Chocolates'),
array( 'id' => 11, 'name' => 'Sweets and Dry Fruits')
);
$array2[] = $array1;
print_r($array2);
outputs...
Array
(
[0] => Array
(
[id] => 8
[name] => Flowers
)
[1] => Array
(
[id] => 10
[name] => Chocolates
)
[2] => Array
(
[id] => 11
[name] => Sweets and Dry Fruits
)
[3] => Array
(
[id] => 12
[name] => Popular
)
)
If you want the data to be at the front, then you need to create an array of that data and then use array_merge()...
$array3 = array_merge(array($array1), $array2);
print_r( $array3);

php multidimensional array iterative recursively

I want to insert extra key before of array if key exists and array is not multi dimensional for example:
Array
(
[0] => Array
(
[_key_] => Array
(
[0] => Array
(
[pub-id-type] => pmid
[value] => 25588809
)
[1] => Array
(
[pub-id-type] => pmc
[value] => 4302133
)
[2] => Array
(
[pub-id-type] => publisher-id
[value] => 1008
)
[3] => Array
(
[pub-id-type] => doi
[value] => 10.1186/s12885-015-1008-4
)
[type_s] => article-id
[id] => 58a6eeedeab2f
)
)
I want:
Array
(
[0] => Array
(
[_key_] => Array
(
[0]=>array(
[0] => Array
(
[pub-id-type] => pmid
[value] => 25588809
)
[1] => Array
(
[pub-id-type] => pmc
[value] => 4302133
)
[2] => Array
(
[pub-id-type] => publisher-id
[value] => 1008
)
[3] => Array
(
[pub-id-type] => doi
[value] => 10.1186/s12885-015-1008-4
)
[type_s] => article-id
[id] => 58a6eeedeab2f
)
)
)
recursively util n depath of _Key_ is found.
The layout is not the best, but howerver ...
try this one:
if(is_array($arr[0][_key_]))
{
$tmp = $arr[0][_key_];
unset($arr[0][_key_]);
$arr[0][_key_][] = $tmp;
}

Difficulty understanding a PHP array structure

I'm looking to manipulate an array that was generated from a MySQL query. The print_r format returns the following:
Array ( [cols] => Array ( [0] => Array ( [label] => time [type] => number ) [1] => Array ( [label] => quantity [type] => number ) ) [rows] => Array ( [0] => Array ( [c] => Array ( [0] => Array ( [v] => 8.8 ) [1] => Array ( [v] => 3 ) ) ) [1] => Array ( [c] => Array ( [0] => Array ( [v] => 8.2 ) [1] => Array ( [v] => 4 ) ) ) [2] => Array ( [c] => Array ( [0] => Array ( [v] => 7.3 ) [1] => Array ( [v] => 1 ) ) ) [3] => Array ( [c] => Array ( [0] => Array ( [v] => 5.7 ) [1] => Array ( [v] => 3 ) ) ) [4] => Array ( [c] => Array ( [0] => Array ( [v] => 4.9 ) [1] => Array ( [v] => 2 ) ) ) [5] => Array ( [c] => Array ( [0] => Array ( [v] => 2.9 ) [1] => Array ( [v] => 1 ) ) ) [6] => Array ( [c] => Array ( [0] => Array ( [v] => 1.6 ) [1] => Array ( [v] => 1 ) ) ) ) )
I put this output into the array beautifier here in order to better understand the structure: http://phillihp.com/toolz/php-array-beautifier/
However, even with this prettier version, I'm unsure how to access specific elements. For example, how would I return just the pair of "8.2","4" bolded above from this array? Any insight into this structure would be greatly appreciated.
Here's the code that generated this array:
$return_structure = array(
'cols' => array (
// array('label' => 'name', 'type' => 'string'),
array('label' => 'time', 'type' => 'number'),
array('label' => 'quantity', 'type' => 'number')
),
'rows' => array()
);
while($row = mysql_fetch_assoc($result)) {
$return_structure['rows'][] = array('c' => array(
//array('v' => $row['name']),
array('v' => $row['time']),
array('v' => $row['quantity']),
));
Thanks in advance for any assistance!!
-Daniel
A good way to get a "pretty" view quickly is to view it in your web browser using the html "pre" tag:
echo "<pre>";
print_r($array);
When accessing a multi dimensional associative array, you can either:
1) use a loop to go through everything, and you may need nested loops to accomplish this. Even using a bunch of foreach loops this could get quite messy and cumbersome.
something like:
foreach ($arrayDImension as $subArray) {
foreach($subArray as $row -> $value) {
//access the rows & values here
}
}
OR
2) access them directly , assuming you know the names of the indexes, etc. You could access them like this:
print_r($array['rows'][3]['c'][0]['v']); // gives you 8.2
print_r($array['rows'][4]['c'][5]['v']); //gives you 4

Arrange an array by weeks

I have an array in which I have field called date and what I need is to separate all these arrays into weeks. Is it posible to do so? Here is my code:
function getWeeks($query){
$postdate = $query['response']['posts']['date'];
return $posts;
}
Here is part of my array:
Array ( [date] => 07/30/12 [message] => test [post_id] => 1 [impressions] => Array ( [0] => 9638 ) [consumptions] => Array ( [0] => 38 ) [storytellers] => Array ( [0] => 6 ) [engaged_users] => Array ( [0] => 31 ) [story_adds] => Array ( [0] => 6 ) [impressions_unique] => Array ( [0] => 4700 ) [comment] => Array ( [0] => 1 ) [like] => Array ( [0] => 5 ) [share] => Array ( [0] => 0 ) [virality] => Array ( [0] => 0 ) [lifetime] => Array ( [0] => 0 ) [affinity] => Array ( [0] => 0 ) )
Array ( [date] => 07/30/12 [message] => test2 [post_id] => 2 [impressions] => Array ( [0] => 10552 ) [consumptions] => Array ( [0] => 47 ) [storytellers] => Array ( [0] => 5 ) [engaged_users] => Array ( [0] => 44 ) [story_adds] => Array ( [0] => 5 ) [impressions_unique] => Array ( [0] => 4982 ) [comment] => Array ( [0] => 0 ) [like] => Array ( [0] => 4 ) [share] => Array ( [0] => 1 ) [virality] => Array ( [0] => 0 ) [lifetime] => Array ( [0] => 0 ) [affinity] => Array ( [0] => 0 ) )
This will loop through each of your post items and group them together if they are in the same week.
View an Example
<?php
$posts = array(
array('date' => '7/30/10', 'title' => 'july post'),
array('date' => '7/19/10', 'title' => 'another post in july'),
array('date' => '7/22/10', 'title' => 'sup, this will be with another post')
);
$grouped_posts = array();
foreach( $posts as $post ) {
// #see http://us2.php.net/manual/en/function.date.php
$week = date('W', strtotime($post['date']));
// create new empty array if it hasn't been created yet
if( !isset($grouped_posts[$week]) ) {
$grouped_posts[$week] = array();
}
// append the post to the array
$grouped_posts[$week][] = $post;
}
print_r($grouped_posts);

How to combine multiple arrays with identical keys into a single array?

I am new to php. I need some help.
I had a array as
Array ( [_] => Array ( [0] => [1] => )
[123_] => Array ( [0] => 123 [1] => )
[1234_] => Array ( [0] => 1234 [1] => )
)
Array ( [_] => Array ( [0] => [1] => )
[12345_] => Array ( [0] => 12345 [1] => )
[1234_] => Array ( [0] => 1234 [1] => )
)
so..whats my problem is i want an array with all these keys and values as
Array ( [_] => Array ( [0] => [1] => )
[123_] => Array ( [0] => 123 [1] => )
[1234_] => Array ( [0] => 1234 [1] => )
[_] => Array ( [0] => [1] => )
[12345_] => Array ( [0] => 12345 [1] => )
[1234_] => Array ( [0] => 1234 [1] => )
)
there would be duplicate keys and values.. but I want all of them as a array.. any help plz..
That is not possible. A PHP array cannot have two identical keys.
As the others said, it's impossible to have a single array with duplicate keys. But you can build an array of array :
<?php
$arr1 = array( '_' => Array ( '0' => '', '1' => ''),
'123_' => Array ( '0' => 123, '1' => ''),
'1234_' => Array ( '0' => 1234, '1' => '')
);
$arr2 = array ( '_' => Array ( '0' => '', '1' => ''),
'12345_' => Array ( '0' => 12345, '1' => ''),
'1234_' => Array ( '0' => 1234, '1' => '')
);
$result = array();
foreach( $arr1 as $key => $val) {
$result[] = array('key'=>$key, 'value'=>$val);
}
foreach( $arr2 as $key => $val) {
$result[] = array('key'=>$key, 'value'=>$val);
}
print_r($result);
?>
Ouput:
Array
(
[0] => Array
(
[key] => _
[value] => Array
(
[0] =>
[1] =>
)
)
[1] => Array
(
[key] => 123_
[value] => Array
(
[0] => 123
[1] =>
)
)
[2] => Array
(
[key] => 1234_
[value] => Array
(
[0] => 1234
[1] =>
)
)
[3] => Array
(
[key] => _
[value] => Array
(
[0] =>
[1] =>
)
)
[4] => Array
(
[key] => 12345_
[value] => Array
(
[0] => 12345
[1] =>
)
)
[5] => Array
(
[key] => 1234_
[value] => Array
(
[0] => 1234
[1] =>
)
)
)
Have a look at PHP's array_merge()-function.

Categories