I var_dump and array and got a value printed, how do i create an array from the result. the array is generated a method and i clearly dont know the structure of the array.
Array ( [0] => gapiReportEntry Object ( [metrics:gapiReportEntry:private] => Array ( [visits] => 4 ) [dimensions:gapiReportEntry:private] => Array ( [year] => 2011 [month] => 07 [day] => 20 ) ) [1] => gapiReportEntry Object ( [metrics:gapiReportEntry:private] => Array ( [visits] => 32 ) [dimensions:gapiReportEntry:private] => Array ( [year] => 2011 [month] => 07 [day] => 13 ) ))
the above is the var_dump result.
I tried to recreate it
$nuarr = array();
$nuarr[0] = array("metrics:gapiReportEntry:private"=>array("visits"=>4),"dimensions:gapiReportEntry:private"=>array("year"=>2011,"months"=>07,"day"=>20));
$nuarr[1] = array("metrics:gapiReportEntry:private"=>array("visits"=>10),"dimensions:gapiReportEntry:private"=>array("year"=>2011,"months"=>07,"day"=>10));
but it doesn't return the same var_dunp value.
Could anyone structure the array for me...
Just assign the new array using assignment operator =
$nuarr = $first_array;
Now the $nuarr is an identical copy of your $first_array.
You can also use the var_export
$nuarr = var_export($first_array, true);
You don't mention why you want to do this. If what you need is just an array-to-string and vice versa mechanism, consider using serialize() and unserialize() rather than var_dump().
If you want to print out an array so that you can clearly see its structure, couldn't you do the following?
echo '<pre>'.print_r($array,1)',</pre>';
I know it isn't using var_dump(), but it would produce the desired result, wouldn't it?
Related
Just a simple question - have this array:
Array
(
[year] => Array
(
[0] => 2019
[1] => 2020
)
[user] => usermail#gmail.com
)
I just want to completely remove [user] => usermail#gmail.com from this Array.
The user will have always only one ocurrence.
I tried unset($filter['user'][0]); also unset($filter['user']); but nothing works.
you can use unset
<?php
$array = Array
(
"year" => Array
(
0 => 2019,
1 => 2020
),
"user"=> "usermail#gmail.com"
);
unset($array['user']);
print_r($array);
?>
Actually "[user] => usermail#gmail.com" is an array index not array.
You need to use this syntax
unset($array['user']);
here $array is the name of array.
Having real issues with this. I want to be able to get a value from this data which is returned via an API.
ie get value by
$CM_user_customfields['Organisation'],
$CM_user_customfields->Organisation.
is that even possible? I have tried loops and rebuilding the array but i always end up with a similar results and perhaps overthinking it.
I can't use the [int] => as the number of custom fields will be changing a lot.
$CM_user_customfields = $CM_details->response->CustomFields ;
echo '<pre>' . print_r( $CM_user_customfields, true ) . '</pre>';
// returns
Array
(
[0] => stdClass Object
(
[Key] => Job Title
[Value] => Designer / developer
)
[1] => stdClass Object
(
[Key] => Organisation
[Value] => Jynk
)
[2] => stdClass Object
(
[Key] => liasoncontact
[Value] => Yes
)
[3] => stdClass Object
...
many thanks, D.
I recommend convert to associative array first:
foreach($CM_user_customfields as $e) {
$arr[$e->Key] = $e->Value;
}
Now you can access it as:
echo $arr['Organisation'];
You can also achieve it by: (PHP 7 can convert stdClass and will do the trick)
$arr = array_combine(array_column($CM_user_customfields, "Key"), array_column($CM_user_customfields, "Value")));
I have a multi-dimensional array and php seems to be returning an array instead of a value when I attempt to access the values directly. What am I doing to cause this?
The array looks like (via print_r):
Array (
[12] => Array ( [2016] => 93083.00 [2015] => 85367.00 [2014] => 69726.00 )
[11] => Array ( [2016] => 66730.00 [2015] => 65548.00 [2014] => 77936.00 )
[10] => Array ( [2016] => 84602.00 [2015] => 112070.00 [2014] => 102104.00 )
)
I'm trying to access values using $arrayname[12][2016] but it is returning Array[2016] instead of 93083.
Is this a simple syntax mistake? Or am I missing part of the concept here? I've been trying to work this problem for hours so maybe I'm missing a simple explanation.
EDIT: the syntax above is actually correct, the issue was in the data entry: I was trying to access a key that didn't exist. I tried to delete the post, but can't since it has been answered.
$arrayname[12] = [2016=>93083.00, 2015=> 85367.00 ]
...
...
echo $arrayname[12][2015] ; // prints 85367
i think your array has one more level. try $arrayname[12][2016][2016] .
I'm sorry if this is newbie question but I don't understand how to access the [ids] value in the JSON array via PHP.
Why is this not working?
$jsonResponse = json_decode($response,true);
print $jsonResponse[2]["ids"];
This is the JSON array:
Array
(
[0] => analytics#gaData
[1] => https://www.googleapis.com/analytics/v3/data/ga?ids=ga:123455&dimensions=ga:eventCategory,ga:eventAction,ga:eventLabel&metrics=ga:visits,ga:pageviews&start-date=2013-01-01&end-date=2020-01-01
[2] => Array
(
[start-date] => 2013-01-01
[end-date] => 2020-01-01
[ids] => ga:123455
[dimensions] => ga:eventCategory,ga:eventAction,ga:eventLabel
[metrics] => Array
(
[0] => ga:visits
[1] => ga:pageviews
)
[start-index] => 1
[max-results] => 1000
)
[3] => 1000
[4] => 9
There doesn't seem to be anything wrong with your code. Your code should be printing, what seems to me, the object ga:123455 if the code is executed. When printing this, this object will be converted to a string. PHP will do (string) ga:123455 (invoke the __toString() method on ga:123455) to convert an object to a string. If this method is absent, there must be a warning that ga:123455 cannot be converted to a string. __toString() might also return an empty string.
I would suggest debugging it by doing print var_dump( $jsonResponse[2]["ids"] ); and print var_dump( (string) $jsonResponse[2]["ids"] );.
This is what i've got now:
Array
(
[0] => Array
(
[0] => Array
(
[id] => 53
[date] => 18 Sep 2010 10:29
[user] => 52
[post] => ytiuy
)
[1] => Array
(
[id] => 55
[date] => 11 Sep 2010 11:14
[user] => 52
[post] => this is a test post :]
)
)
[1] => Array
(
[0] => Array
(
[id] => 56
[date] => 4 Sep 2010 03:19
[user] => 55
[post] => pppost :DD:D:D:D
)
)
)
I want to remove the first two "steps" in the array, and then sort the array by the 'date' value, like this:
Array
(
[0] => Array
(
[id] => 56
[date] => 4 Sep 2010 03:19
[user] => 55
[post] => pppost :DD:D:D:D
)
[1] => Array
(
[id] => 55
[date] => 11 Sep 2010 11:14
[user] => 52
[post] => this is a test post :]
)
[2] => Array
(
[id] => 53
[date] => 18 Sep 2010 10:29
[user] => 52
[post] => ytiuy
)
)
Any ideas?
Thanks a bunch, appreciate all help! :)
EDIT: I should also mention that the amount of arrayitems will not always be the same.
You should be able to use an accumulator pattern with the array_merge function to merge all the lower level arrays together.
$result = array();
foreach ($oldarray as $child)
{
$result = array_merge($result, $child);
}
Finally, you can use the user defined sort function to sort the whole thing.
An alternative to Don Kirby's solution would be to use an SplMaxHeap which would allow you to iterate and sort in one go:
class PostHeap extends SplMaxHeap
{
public function compare($post, $other)
{
return strtotime($post['date']) - strtotime($other['date']);
}
}
$postHeap = new PostHeap;
foreach($posts as $subArray) {
foreach($subArray as $post) {
$postHeap->insert($post);
}
}
The $postHeap would then contain the posts in descending date order, e.g. newest date first. You can use the code in the compare function if you want to use usort instead. The order will be ascending then though.
Do you have two arrays? Or more? Are they already sorted? If so, you can use that to combine them more efficiently. If not, you probably need to sort them first.
Roughly:
Sort your input arrays (optionally)
Scan your input arrays for the lowest value, copy that value into your new array, delete the value from the input array.
Repeat until all your input arrays are empty.
Of course, if you don't care about performance at all you could simply combine all the arrays and then sort that.
And for sorting you may want to use: http://www.php.net/manual/en/function.sort.php#99700
#Don Kirkby: Indeed: It's basically a mergesort, but it only works on already sorted arrays. If they're both unsorted you're probably better off with combining them and using quicksort instead.