last day (14 days later) » 

9:49 AM
-3
Q: PHP array to notation with []

abinaxIs there any PHP predefined function that transform an array to this notation: Example: From this Array ( [0] => Array ( [key1] => 10 [key2] => 11 ) [1] => Array ( [key1] => 12 [key2] => 13 ) ) To this [ [10,11] , [12,13] ] Note: I m looking for a predefined function...

 
Why was this closed as a duplicate? I don't see them wanting to pass variables between PHP and JavaScript, just a badly tagged question.
 
user8208945
@deceze why it is a duplicate?
 
you want [ [10,11] , [12,13] ] as output - so as a string?
 
After rewriting this and removing the "javascript" related keywords, it does look less like a dupe, yes. But the answer is simple and explained from several angles in the duplicate: json_encode. Or what exactly is it you're asking…?
 
user8208945
@Jeff yeah, i'ts easy to code with loops...but I m looking for a predefined function!
 
user8208945
9:49 AM
@deceze It was a mistake, there is no relation with javascript
 
json_encode kinda gives you the result. But since the inner arrays have keynames it'll look different: [{"key1":10,"key2":11},{"key1":12,"key2":13}]
 
@deceze if I understand correctly, they want a function to change the array() notation to [] notation within PHP
 
Then you want stackoverflow.com/a/10492870/476, I don't think the "notation" part is very important here.
 
Not quite that either @deceze I believe
 
@Albzi There's no functional difference between array() and []
 
9:49 AM
@deceze I know that :) I am just relaying what the OP is asking. It's technically not a duplicate question, whether that makes it a good question however, is totally different.
 
@Albzi In case you didn't notice, I added a second duplicate target that clearly fits.
 
I saw, like I said before though, it's not exactly what OP is asking for. They're not asking to reset their keys in their arrays. They're asking to change their array() to []. Technically different (although useless) @deceze
 
@Albzi Not verbatim, but it is what they want to achieve.
 
in Tavern on the Meta on Meta Stack Exchange Chat, 1 min ago, by Albzi
If Deceze is in there, can someone just say we'll agree to disagree
 
@rene Noted :)
 
10:09 AM
what's the difference between the 2?
 
user8208945
I found the solution by my self:
 
user8208945
function function_array($array){
    $result = "[";
    foreach ($array as $value){
        $result .=(string)$value.", ";
    }
    $result=rtrim($result,", ");
    $result .= "]";
    return $result;
}

function function_array_2d($array){
    $result = Array();
    foreach ($array as $value) {
        array_push($result, function_array($value));
    }
    return function_array($result);
}
 
user8208945
this second function do exactly what I need
 
@abinax Uhm… json_encode(array_map('array_values', $array))…?!
 
user8208945
@deceze your function give this [["11","3124"],["12","3021"],["10","2991"]]
 
user8208945
10:23 AM
while I need this [[11, 3124], [12, 3021], [10, 2991]]
 
user8208945
without the " " !!
 
Well, if your values are strings, then you should ensure they're integers first.
And after all, you do want to produce JSON, do you?
 
user8208945
If you have time to read this :3 ... I will explain all the strory
 
user8208945
I have data in php array, and I need to draw it with google charts
 
user8208945
google charts use javascript with the notation that I looking for
 
user8208945
10:26 AM
so, I have choosed to transform the php array into a string that javascript knows,
 
user8208945
echo the result into the script and draw the chart
 
user8208945
Now, you know
 
So how was this not about passing data from PHP to Javascript…?
And as for the solution, you should not be cobbling together JSON by hand. The line I show above is exactly what you want. You simply need to make sure your array is actually in the shape that you want in PHP before JSON-encoding it.
I don't know why your numbers are strings. Perhaps you can fix that somewhere much earlier so you're actually dealing with numbers.
 
user8208945
ok, thank you very much
 
user8208945
now you understand me
 
10:30 AM
If not, you need to cast them to numbers, e.g.: array_map(function ($a) { return array_map('intval', $a); }, $array)
 
user8208945
nice
 

last day (14 days later) »