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
Related
I need help modifying arrays to reach a desired structure. I am sorry the example structure maybe big, but i wanted to show the structure better.
I have a target Array like below that I need to generate
[bf_1040242] => Array
(
[326] => Just some Information. Additional Text
[17565] => Array
(
[0] => 2
[1] => 1
[2] => 3
)
[other] => Array
(
[17565] => Testing
[28623] =>
[42284] => Something Else
)
[597] => 1
[327] => This is some text
[328] => asdasd
[11880] => wwwww
[329] => xxxxx
[28622] => 2
[42283] => 1
[42284] => Array
(
[0] => 2
[1] => 3
[2] => 4
)
The data for me to generate this comes in a different format where the structure of these values are in string.
Array
(
[0] => Array
(
[name] => bf_1040242[326]
[value] => Just some Information. Additional Text
)
[1] => Array
(
[name] => bf_1040242[17565][]
[value] => 2
)
[2] => Array
(
[name] => bf_1040242[17565][]
[value] => 1
)
[3] => Array
(
[name] => bf_1040242[17565][]
[value] => 3
)
[4] => Array
(
[name] => bf_1040242[other][17565]
[value] => Testing
)
[5] => Array
(
[name] => bf_1040242[597]
[value] => 1
)
[6] => Array
(
[name] => bf_1040242[327]
[value] => This is some text
)
[7] => Array
(
[name] => bf_1040242[328]
[value] => asdasd
)
[8] => Array
(
[name] => bf_1040242[11880]
[value] => wwwww
)
[9] => Array
(
[name] => bf_1040242[329]
[value] => xxxxx
)
[10] => Array
(
[name] => bf_1040242[28622]
[value] => 2
)
[11] => Array
(
[name] => bf_1040242[other][28623]
[value] =>
)
[12] => Array
(
[name] => bf_1040242[42283]
[value] => 1
)
[13] => Array
(
[name] => bf_1040242[42284][]
[value] => 2
)
[14] => Array
(
[name] => bf_1040242[42284][]
[value] => 3
)
[15] => Array
(
[name] => bf_1040242[42284][]
[value] => 4
)
[16] => Array
(
[name] => bf_1040242[other][42284]
[value] => Something Else
)
)
In the above array, the key 'name' represents the structure of individual array in string format and the 'value' holds the value that the array structure needs to be updated to.
This is have managed to get done by using
$extra = []
foreach ($mainArray as $key => $value)
{
parse_str($value['name'], $tempArr);
$m = $this->multiArr($k, $value);
array_push($extra, $m);
}
protected function multiArr($k, $val)
{
foreach($k as $key => $value)
{
if(is_array($value) ) $k[$key] = $this->multiArr($k[$key], $val);
if(!is_array($value))
{
$k[$key] = (string) $val['value'];
}
}
return $k;
}
$m holds individual array values like
Array
(
[bf_1040242] => Array
(
[other] => Array
(
[42284] => Something Else
)
)
)
Using these individual array values i want to create the the Main required array so i used
array_push($extra, $m);
but this adds each $m array below each other instead of updating the way expected above.
Below is the way it is coming in my end.
Array
(
[0] => Array
(
[bf_1040242] => Array
(
[326] => Just some Information. Additional Text
)
)
[1] => Array
(
[bf_1040242] => Array
(
[17565] => Array
(
[0] => 2
)
)
)
[2] => Array
(
[bf_1040242] => Array
(
[17565] => Array
(
[0] => 1
)
)
)
[3] => Array
(
[bf_1040242] => Array
(
[17565] => Array
(
[0] => 3
)
)
)
[4] => Array
(
[bf_1040242] => Array
(
[other] => Array
(
[17565] => Testing
)
)
)
[5] => Array
(
[bf_1040242] => Array
(
[597] => 1
)
)
[6] => Array
(
[bf_1040242] => Array
(
[327] => This is some text
)
)
[7] => Array
(
[bf_1040242] => Array
(
[328] => asdasd
)
)
[8] => Array
(
[bf_1040242] => Array
(
[11880] => wwwww
)
)
[9] => Array
(
[bf_1040242] => Array
(
[329] => xxxxx
)
)
[10] => Array
(
[bf_1040242] => Array
(
[28622] => 2
)
)
[11] => Array
(
[bf_1040242] => Array
(
[other] => Array
(
[28623] =>
)
)
)
[12] => Array
(
[bf_1040242] => Array
(
[42283] => 1
)
)
[13] => Array
(
[bf_1040242] => Array
(
[42284] => Array
(
[0] => 2
)
)
)
[14] => Array
(
[bf_1040242] => Array
(
[42284] => Array
(
[0] => 3
)
)
)
[15] => Array
(
[bf_1040242] => Array
(
[42284] => Array
(
[0] => 4
)
)
)
[16] => Array
(
[bf_1040242] => Array
(
[other] => Array
(
[42284] => Something Else
)
)
)
)
I am not sure if array_push is what i need to use, or something else.
Also, is there a cleaner or a more efficient way to this.
Any help is much appreciated, Thanks
Normally you'd use array_merge_recursive for this, but it wont preserve named keys, so I wrote a little array_join function to make life easier :)
function array_join($value, &$result) {
if (!is_array($value)) {
$result = $value;
return;
}
foreach ($value as $k => $v) {
array_join($v, $result[$k]);
}
}
$result = [];
foreach ($mainArray as $entry)
{
parse_str($entry['name'] . '=' . $entry['value'], $nameArray);
array_join($nameArray, $result);
}
We simply call array_join recursively until the value is no longer an array. Notice the & in &$result of array_join. We pass the reference of our current position in the array.
To make full use of parse_str I've added the value aswell by appending =<value>
Working example.
Pro tip for your next post. Use var_export to give us an array, which can be copy pasted as code :)
EDIT
More simple solution with escaped values for "evil" input.
foreach ($mainArray as $entry)
{
$stringVarArray[] = $entry['name'] . '=' . urlencode($entry['value']);
}
parse_str(implode('&', $stringVarArray), $result);
var_dump($result);
Working example.
Array
(
[3M] => Array
(
[0] => Array
(
[name] => 3M
[price] => 158.15
)
[440] => Array
(
[name] => 3M
[price] => 156.69
)
)
[AO Smith] => Array
(
[1] => Array
(
[name] => AO Smith
[price] => 47.29
)
[441] => Array
(
[name] => AO Smith
[price] => 47.19
)
)
So I have an Array that is above^^^. I would like to get it into a condensed array format. I need a function that loops through the above and outputs it in the format below.
Array
(
[3M] => Array
(
[price1] => 158.15
[price2] => 156.69
)
[AO Smith] => Array
(
[price1] => 47.29
[price2] => 47.19
)
)
Above is how I would like the data oriented.
Thanks for the help.
What you'll find is the format you want is not good and not as usable or flexible. This however will give you a better format. name and price are descriptive, price1 and price2 are no different than 0 and 1:
foreach($array as $key => $values) {
$result[$key] = array_column($values, 'price');
}
Yields:
Array
(
[3M] => Array
(
[0] => 158.15
[1] => 156.69
)
[AO Smith] => Array
(
[0] => 47.29
[1] => 47.19
)
)
I have an array like below
Array
(
[0] => Array
(
[text] => one
[mp3] => 1.mp3
)
[1] => Array
(
[text] => two
[mp3] => 2.mp3
)
[2] => Array
(
[text] => three
[mp3] => 3.mp3
)
)
And I have another array with index to sort array(1,0,2) ,So With these I want following
Array
(
[0] => Array(
[text] => two[mp3] => 2. mp3
)
[1] => Array(
[text] => one[mp3] => 1. mp3
)
[2] => Array(
[text] => three[mp3] => 3. mp3
)
)
I googled and Found Few solutions on stackoverflow , But None seems to be successful for me
$order=array(1,0,2);
$orderedarray = array_merge(array_flip($order),$myarr);
$myarr = $orderedarray;
print_r($myarr);
Which outputs following
(
[0] => 0
[1] => 1
[2] => 2
[3] => Array
(
[text] => one
[mp3] => 1.mp3
)
[4] => Array
(
[text] => two
[mp3] => 2.mp3
)
[5] => Array
(
[text] => three
[mp3] => 3.mp3
)
)
You need to use array_multisort.
$order =[1,0,2];
array_multisort($myarr, $order);
Var_dump($myarr);
https://3v4l.org/cCT9d
im using cakephp 1.3. Im trying to sort an array using Set::sort() function but is not working.. any idea on how to do this? below is the array im using.
Array (
[0] => Array
(
[Group] => Array
(
[name] => Team A
)
[Members] => Array
(
[0] => Array
(
[name] => George
[Code] => Array
(
[name] => C
)
)
[1] => Array
(
[name] => Hall
[Code] => Array
(
[name] => A
)
)
[2] => Array
(
[name] => Mike
[Code] => Array
(
[name] => B
)
)
)
)
im sorting the array using this :
$data = Set::sort($data, '{n}.Members.{n}.Code.name', 'asc');
im expecting an output like this:
Array
(
[0] => Array
(
[Group] => Array
(
[name] => Team A
)
[Members] => Array
(
[0] => Array
(
[name] => Hall
[Code] => Array
(
[name] => A
)
)
[1] => Array
(
[name] => Mike
[Code] => Array
(
[name] => B
)
)
[2] => Array
(
[name] => George
[Code] => Array
(
[name] => C
)
)
)
)
The sorting does not take in effect.how can i do this? any idea?
Using only Set::sort() its no doable. You can you this:
$result = array();
foreach($a as $arr) {
$res = Set::sort($arr['Member'], '{n}.Code.name', 'asc');
$result[] = array(
'Group' => $arr['Group'],
'Member' => $res
);
}
pr($result);
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
)
)
)