Remove keys from multidimesional array - php

I have an array name $json_output.
array(3) {
["ProductsSummary"]=>
array(2) {
["TotalPages"]=>
int(2)
["CurrentPage"]=>
int(1)
}
["Products"]=>
array(60) {
[0]=>
array(3) {
["LastShopUpdate"]=>
string(26) "/Date(1382716320000+0200)/"
["Score"]=>
float(0.2208696)
["ProductId"]=>
int(1306413101)
["ArticleNumber"]=>
}
[1]=>
array(3) {
["LastShopUpdate"]=>
string(26) "/Date(1382716320000+0200)/"
["Score"]=>
float(0.2208696)
["ProductId"]=>
int(1306413101)
["ArticleNumber"]=>
}
And so on. I need to unset ProductId and LastShopUpdate from each one.
What i tried:
<?php
foreach($json_output["Products"] as $bla)
unset($bla['ArticleNumber'], $bla['LastShopUpdate']);
?>
But it is not working. How could I do this?

When looping over an array using foreach, a copy is usually made. Changing something in the copy of course has no effect on the original. Try this:
foreach($json_output["Products"] as & $bla)
unset($bla['ArticleNumber'], $bla['LastShopUpdate']);
The & causes $bla to be a reference instead of a copy. Therefore it should resolve your problem.

Related

(PHP) var_dumping an object in structured format.

I need to analyze an object in my code, but when I do a var_dump (or print_r) it just prints the object out with no structure, for example:
[0]=> object(simple_html_dom_node)#2185 (9) { ["nodetype"]=> int(1) ["tag"]=> string(3) "div" ["attr"]=> array(1) { ["class"]=> string(36) "element element--collection internal" } ["children"]=> array(2) { [0]=> object(simple_html_dom_node)#2187 (
I need to see it in a more structured format so I can see what is going on, i.e.:
object(simple_html_dom_node)#2185 (9) {
["nodetype"]=> int(1)
["tag"]=> string(3) "div"
["attr"]=> array(1)
{
["class"]=> string(36) "element element--collection internal"
}
["children"]=> array(2) {
[0]=> object(simple_html_dom_node)#2187 (9)
Does anyone know how to do this?
The format you want is actually how var_dump() prints the object. The problem is that you're doing it in an HTML document, and the browser reformats it.
If you put it inside a <pre> tag, the browser will leave the formatting alone. So:
echo "<pre>"; var_dump($object); echo "</pre>";
Try using var_export(), this function will give you a more readable structure of the object or data.

How to loop through multidimensional array?

I'm currently getting a JSON response from a company's API and converting it into a PHP array like this:
$api_url = file_get_contents('http://example.com');
$api_details = json_decode($api_url, true);
When I run var_dump on $api_details, I am getting this:
array(2) {
["metadata"]=>
array(5) {
["iserror"]=>
string(5) "false"
["responsetime"]=>
string(5) "0.00s"
["start"]=>
int(1)
["count"]=>
int(99999)
}
["results"]=>
array(3) {
["first"]=>
int(1)
["result"]=>
array(2) {
[0]=>
array(4) {
["total_visitors"]=>
string(4) "3346"
["visitors"]=>
string(4) "3249"
["rpm"]=>
string(4) "0.07"
["revenue"]=>
string(6) "0.2381"
}
[1]=>
array(4) {
["total_visitors"]=>
string(6) "861809"
["visitors"]=>
string(6) "470581"
["rpm"]=>
string(4) "0.02"
["revenue"]=>
string(7) "13.8072"
}
}
}
}
I'm trying to do 2 things and can't figure out how to do either with a multidimensional array.
I need to check to see if metadata > iserror is false. If it is not false, I want to show an error message and not continue with the script.
If it is false, then I wants to loop through the results of results > result and echo the total_visitors, visitors, etc for each of them.
I know how to echo data from array, I guess I'm just getting confused when there's multiple levels to the array.
Anyone that can point me in the right direction would be much appreciated :)
You can iterate over arrays using foreach. You can read up on it here: http://php.net/manual/en/control-structures.foreach.php
Since you're using associative arrays, your code will look something like this:
if ($arr['metadata']['iserror']) {
// Display error here
} else {
foreach($arr['results']['result'] as $result) {
echo $result['total_visitors'];
echo $result['visitors'];
}
}
You'll have to tweak the code to fit exactly what you're doing, but this should get you over the line.
Hope that helps!

Php array output confusion

In PHP when I do
var_dump($galleryCategoriesThumb);
Output is
array(1) {
[0]=>
array(1) {
["Gallery"]=>
array(3) {
["id"]=>
string(3) "190"
["photofile"]=>
string(6) "50.jpg"
["gallery_category_id"]=>
string(2) "58"
}
}
}
When I do
var_dump($galleryCategoriesThumb["photofile"]);
I get NULL output. I tried various other options also.
All I want to do is echo ["photofile"]. Please help.
Try $galleryCategoriesThumb[0]['Gallery']["photofile"]
Try var_dump($galleryCategoriesThumb[0]["Gallery"]["photofile"]);.
It's a 3 dimensions array.
It is expected that var_dump($galleryCategoriesThumb["photofile"]) returns NULL because "photofile" key does not exist in $galleryCategoriesThumb.
To access "photofile", you need a 'full path' in the array as posted by others
var_dump($galleryCategoriesThumb[0]["Gallery"]["photofile"]);.

A list of arrays contained in a master array PHP

I hope I can make this question clear enough.
I'm looking to put a list of arrays inside one master array, dynamically, so that it looks like this:
masterarray {
array1
{ [0]=>VAL1 [1]=>VAL2 }
array2
{ [0]=>VAL1 [1]=>VAL2 }
array3
{ [0]=>VAL1 [1]=>VAL2 }
}
I've tried, but I could only get it to look like this:
array(1) { [0]=> array(2) { [0]=> string(1) "1" [1]=> string(13) "CODE" } }
array(2) { [0]=> array(2) { [0]=> string(1) "1" [1]=> string(13) "CODE" } [1]=> array(2) { [0]=> string(1) "1" [1]=> string(13) "CODE" } }
array(3) { [0]=> array(2) { [0]=> string(1) "1" [1]=> string(13) "CODE" } [1]=> array(2) { [0]=> string(1) "1" [1]=> string(13) "CODE" } [2]=> array(2) { [0]=> string(1) "1" [1]=> string(13) "CODE" } }
And that's definitely not what I'm aiming for. Nothing seems contained. I need the format specified above.
I'm using the explode function on a string pulled from a file to make this table of arrays (I think you call it that)
Here is the code I'm using that's not working.
$variabledebugging = file("FILE.TXT");//LOOK IN THIS FILE FOR THE NUMBER AND SET IT TO A VAR.
$i=0;
foreach($variabledebugging as $placeholder){
$variabledebuggingtbl[] = explode("\t",$variabledebugging[$i]);
var_dump($variabledebuggingtbl);
$i++;
}
I've tried a couple of different variations, but that's the one I'm using now.
To be clear, that file being pulled (each line as a value in an array) has 2 things written to each line, separated by a tab character, so that's the system I'm going on.
Thank you! I'm sure this is a simple task, I just can't think it through.
Oh and while I'm at is there a way to make debugging more readable?
You ARE getting the right result. The reason it seems wrong is that you are running var_dump inside the loop. And why don't you use the $placeholder variable?
$variabledebugging = file("FILE.TXT");
foreach($variabledebugging as $placeholder){
$variabledebuggingtbl[] = explode("\t", $placeholder);
}
var_dump($variabledebuggingtbl);
I'm not sure what you mean by "making debugging more readable", but if you want some linebreaks and indentation you should just look in the generated HTML code. var_dump do add spacing to make it readable but it is ignored by the web browser. If you don't want to read the HTML source, just add your var_dump to a <pre> element.

PHP array minor problem

I'm really not sure how to explain this. It's so simple I can't fathom why it's not working.
I have a loop. It puts a bunch of strings into an array. If I fill a single variable with any given string, it will output it perfectly.
But filling an array with the strings will make it give me the dreaded:
Array Array Array Array Array Array Array Array
Note: my strings are not all 'Array'.
The way I loop is:
while(...)
{
$arr[] = $resultFromLoop;
}
Here is my var_dump.
array(1) {
["tagName"]=>
string(5) "magic"
}
array(1) {
["tagName"]=>
string(4) "nunu"
}
array(1) {
["tagName"]=>
string(5) "books"
}
array(1) {
["tagName"]=>
string(0) ""
}
array(1) {
["tagName"]=>
string(3) "zzz"
}
array(1) {
["tagName"]=>
string(4) "grey"
}
array(1) {
["tagName"]=>
string(3) "new"
}
array(1) {
["tagName"]=>
string(6) "flight"
}
This is because you're working with array as with a string.
It puts a bunch of strings into an array.
Nope, there are no strings. I already gave you a magic var_dump($resultFromLoop) function, but you're too lazy to use it for debugging your code (because there is SO, where you can ask any question and don't bother yourself with thinking)

Categories