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.
Related
Say I have the following array (this comes from a var_dump):
($defaults = )array(3) {
["sitewide_typography_title"]=>
array(2) {
["font-family"]=>
string(16) "Playfair Display"
["variant"]=>
string(7) "regular"
}
["sitewide_typography_text"]=>
array(2) {
["font-family"]=>
string(6) "Roboto"
["variant"]=>
string(3) "300"
}
["sitewide_typography_btn"]=>
array(2) {
["font-family"]=>
string(6) "Roboto"
["variant"]=>
string(3) "300"
}
}
I guess this is an easy question, but I really can't find the answer. I think my googling skills failed me in this, but how do I get the font-family value for sitewide_typography_title?
Thanks a lot in advance!
It's simple
$defaults['sitewide_typography_title']['font-family'];
To echo it out like this
echo $defaults['sitewide_typography_title']['font-family'];
Should output
Playfair Display
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!
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.
I cannot seem to figure out how to send to a group in my list using mailChimp API
My code looks like this:
$conditions = array('field'=>'interests-1', 'op'=>'all', 'value'=>'myGroup');
$opts = array('match'=>'any', 'conditions'=>$conditions);
$retval = $api->campaignSegmentTest($listId, $opts);
But this yields bool(false). When fetched by
$retval = $api->listInterestGroupings($listId);
my list looks like this:
array(1) {
[0]=>
array(5) {
["id"]=>
int(1)
["name"]=>
string(10) "myList"
["form_field"]=>
string(5) "radio"
["display_order"]=>
string(1) "0"
["groups"]=>
array(5) {
[0]=>
array(4) {
["bit"]=>
string(1) "1"
["name"]=>
string(9) "myGroup"
["display_order"]=>
string(1) "1"
["subscribers"]=>
int(1)
}
[1]=>
array(4) {
["bit"]=>
string(1) "2"
["name"]=>
string(9) "myGroup_2"
["display_order"]=>
string(1) "2"
["subscribers"]=>
int(1)
}
}
}
}
I have looked in the API documentation and searched for the answer, but cannot seem to figure it out. Grateful for help!
Looks like you are using the PHP wrapper - the first thing to do, like the examples included with it do, is to check for any errors by looking at $api->errorCode before messing with the $retval.
When you do that I'm certain you will see an error telling you that you haven't passed a proper "conditions" parameter since it is an array of arrays, not an array.
This question already has answers here:
Make var_dump look pretty [duplicate]
(16 answers)
Closed 1 year ago.
I've seen some online pretty print modules for code. Anyone know of one that will format a multi-dimensional array into readable display?
Example, translate this:
array(83) { [0]=> array(2) {
["name"]=> string(11) "CE2 Options"
["type"]=> string(5) "title" } [1]=>
array(1) { ["type"]=> string(4) "open"
} [2]=> array(5) { ["name"]=>
string(8) "Template" ["desc"]=>
string(638) "test description"
["id"]=> string(9) "my_theme"
["type"]=> string(14) "selectTemplate"
["options"]=> array(13) {
Into this...
array(83) {
[0]=> array(2) { ["name"]=> string(11) "My Options" ["type"]=> string(5) "title" }
[1]=> array(1) { ["type"]=> string(4) "open" }
[2]=> array(5) {
["name"]=> string(8) "Template"
["desc"]=> string(638) "Test description"
["id"]=> string(9) "my_theme"
["type"]=> string(14) "selectTemplate"
["options"]=> array(13) {
[0]=> string(10) "test"
If you are dumping it to HTML document use the
<pre></pre>
it does exactly that.
If you want a nicer output than var_dump , then check out the alternatives listed here:
A more pretty/informative Var_dump alternative in PHP?
Particularily http://krumo.sourceforge.net/ provides a much more accessible DHTML view for variable dumps. (It requires an extra include() though.)
And if you actually want to keep the generated output as static html, you might have to write a smallish wrapper script.
the pretty version is just what you get when you have XDebug installed and html_errors is set to On. Then you use var_dump($array). And make sure you set children and depth to what you need. there you go