I know this is a newbie question,
I extracted some preformatted json out of javascript, that looks like this:
[[[455837.99,2896882.36],[455862.44,2896888.35],[455868.79,2896860.14],[455864.3,2896852.78],[455845.76,2896848],[455837.99,2896882.36]]]
I tried an online formatted that shows me the array looks something like this:
Array
(
[0] => Array
(
[0] => Array
(
[0] => 455837.99
[1] => 2896882.36
)
[1] => Array
(
[0] => 455862.44
[1] => 2896888.35
)
[2] => Array
(
[0] => 455868.79
[1] => 2896860.14
)
[3] => Array
(
[0] => 455864.3
[1] => 2896852.78
)
[4] => Array
(
[0] => 455845.76
[1] => 2896848
)
[5] => Array
(
[0] => 455837.99
[1] => 2896882.36
)
)
)
All the third [0]'s are what I need as an X, so everything that starts with 455. and All the [1]'s are the Y's. I would like these two to be grouped together, the respective X and Y.
My problem is I am not sure how to access them out of the area, I have tried this:
preg_match_all('/\[{3}.*\]{3}/', $data, $matches);
$arr=array();
foreach ($matches[0] as $match)
{
$arr[]=json_decode($match);
}
echo '<br>';
$newmatch = json_decode($match);
I would really appreciate if someone can point me in the direction of being able to have a loop that basically echo's the X and Y values, whenever I try and echo something from the array I get:
1) Array to string conversion
I am not sure if that is because of the triple brackets, that its an array in an array in an array.
$data = "[[[455837.99,2896882.36],[455862.44,2896888.35],[455868.79,2896860.14],[455864.3,2896852.78],[455845.76,2896848],[455837.99,2896882.36]]]";
$data_array = json_decode($data);
if (is_array($data_array) && array_key_exists(0, $data_array)) {
foreach ($data_array[0] as $element) {
$x = array_key_exists(0, $element) ? $element[0] : "N/A";
$y = array_key_exists(1, $element) ? $element[1] : "N/A";
printf("X: %s | Y: %s\n", $x, $y);
}
}
Related
I'm trying to change my array from this:
Array
(
[0] => Array
(
[BID_OPEN] => Array
(
[0] => 0.718282
)
)
[1] => Array
(
[BID_CLOSE] => Array
(
[0] => 1.654545
)
)
[2] => Array
(
[BID_OPEN] => Array
(
[0] => 1.654878
)
)
)
in to this:
Array
(
[BID_OPEN]
(
[0] => 0.718282
[1] => 1.654878
)
[BID_CLOSE]
(
[0] => 1.654545
[1] => 1.645845
)
)
I'm not sure how to begin. My code:
foreach($array as $keys=>$values)
{
if(!empty($array [$c]['BID_OPEN']))
{
$inital_part1 = array("BID_OPEN", $array [$c]['BID_OPEN']);
}
else
{
echo '';
}
if(!empty($array [$c]['BID_CLOSE']))
{
$inital_part2 = array("BID_CLOSE", $array [$c]['BID_CLOSE']);
}
else
{
echo '';
}
$array1[] = $inital_part1;
$array1[] = $inital_part2;
$c++;
}
I seem to get double outputs, so the foreach when I build arrays is giving me two times the required output. Google reckons it's because I have an array in my array somewhere but I'm precisely sure I don't.
The array came from an object stdclass and I don't know what that is, have googled but haven't found anything useful. Also I'm able to get some figures but only the initial values are correct, the rest of the data doesn't seem to come through. No doubt it's because I used an index[0] to get it working.
After hours any help would be great thanks.
As long as you have told us everything about your input array it can be done quite simply like this
<?php
$in = [ ['BID_OPEN' => [0.718282]],
['BID_CLOSE' => [1.654545]],
['BID_OPEN' => [1.654878]]
];
print_r($in);
$new = []; // new array we are building
foreach ($in as $abid) {
if (array_key_exists('BID_OPEN', $abid) ) {
$new['BID_OPEN'][] = $abid['BID_OPEN'][0];
}
if (array_key_exists('BID_CLOSE', $abid) ) {
$new['BID_CLOSE'][] = $abid['BID_CLOSE'][0];
}
}
print_r($new);
THE INPUT ARRAY: Is like yours
Array
(
[0] => Array
(
[BID_OPEN] => Array
(
[0] => 0.718282
)
)
[1] => Array
(
[BID_CLOSE] => Array
(
[0] => 1.654545
)
)
[2] => Array
(
[BID_OPEN] => Array
(
[0] => 1.654878
)
)
)
RESULT:
Array
(
[BID_OPEN] => Array
(
[0] => 0.718282
[1] => 1.654878
)
[BID_CLOSE] => Array
(
[0] => 1.654545
)
)
$c = 0;
$array1['BID_OPEN'] = [];
$array2['BID_CLOSE'] = [];
foreach($vartttttt as $tunips=>$ert)
{
$d = 0;
foreach($ert as $erts=>$val)
{
//$array[] = $erts;
if($erts == 'BID_OPEN')
{
array_push($array1['BID_OPEN'], $val[0]);
}
if($erts == 'BID_CLOSE')
{
array_push($array2['BID_CLOSE'], $val[0]);
}
$d++;
}
$c++;
}
$array = array_merge($array1, $array2);
I have an array which looks like this:
Array
(
[0] => Array
(
[0] => TE=140414100000 cd =AB1234 ggg =1234567 gbh =2
[7] => nd: DA1AAAAAAAAAA: TD = 140414:
)
[1] => Array
(
[0] => TE=140414100000 cd =AB1234 ggg =1234567 ghb =2
[7] => nd: DA1AAAAAAAAAA: TD = 140414:
)
)
what I am trying to acomplish is to parse data within each sub array and create a new multidimensional array with the parsed data.
Example: the data in parentheses below is what should be returned in new multidimensional array
Array
(
[0] => Array
(
[0] => te=(140414100000) cd =AB(1234) ggg =1234567 ghb =2
[7] => nd: DA(1)(AAAAAAAAAA): TD = (140414):
)
[1] => Array
(
[0] => te=(140414100000) cd =AB(1234) ggg =1234567 ghb =2
[7] => nd: DA(2)(BBBBBBBBBB): TD = (140414):
)
)
What I want to return:
Array
(
[0] => Array
(
[0] => 140414100000
[1] => 1234
[2] => 1
[3] => AAAAAAAAAA
[4] => 140414
)
[1] => Array
(
[0] => 140414100000
[1] => 1234
[2] => 2
[3] => BBBBBBBBBB
[4] => 140414
)
).
So my question is what would be the best way to acomplish this?
This is what I have come up with. It works, however is seems very inefficient as it adds a lot of empty arrays which have to be cleaned up.
foreach($new as $key => $val){
foreach($val as $res){
preg_match_all('%te=([0-9]{12})\s%',$res,$matches);
$out[$key][] = $matches[1][0];
preg_match_all('%cd\s+=AB([0-9]{4})%',$res,$matches);
$out[$key][] = $matches[1][0];
preg_match_all('%nd:\sDA([0-9]{1})%',$res,$matches);
$out[$key]['node'] = $matches[1][0];
preg_match_all('%nd:\sDA[0-9]{1}([a-zA-Z]{10,14}):%',$res,$matches);
$out[$key]['rset'] = $matches[1][0];
preg_match_all('%td\s=\s([0-9]{6}):%',$res,$matches);
$out[$key]['trdt'] = $matches[1][0];
}
}
foreach($out as $v){
$v = array_values(array_filter($v));
$return[] = $v;
}
return $return;
Thanks in advance.
UPDATED:
This worked and is much more efficient. Thanks for the example Shankar
foreach($new as $key => $val){
$v = implode('', $val);
preg_match_all("%te=([0-9]{12})|cd\s+=AB([0-9]{4})|nd:\sDA([0-9]{1})|([A-Z]{3,7}):|td=\s([0-9]{6}):%",$v,$matches);
$new_array[$key]['time'] = $matches[1][0];
$new_array[$key]['code'] = $matches[2][1];
$new_array[$key]['sp'] = $matches[3][2];
$new_array[$key]['rset'] = $matches[4][3];
$new_array[$key]['trfdt'] = $matches[5][4];
}
echo "<pre>";
print_r($new_array);
echo "</pre>";
Loop through your array and implode each array element and use a preg_match_all() to capture all the entries bewteen ( and ) and then pass those matches to your new array.
foreach($arr as $k=>$arr1)
{
$v = implode('',$arr1);
preg_match_all('^\((.*?)\)^', $v, $matches);
$new_arr[]=$matches[1];
}
print_r($new_arr);
Working Demo
Consider the array is:
Array
(
[Page-1] => Array
(
[0] => Array
(
[0] => Cat-1
[1] => Item-1
)
)
[Page-2] => Array
(
[0] => Array
(
[0] => Cat-2
[1] => Item-2
)
[1] => Array
(
[0] => Cat-3
[1] => Item-3
)
[2] => Array
(
[0] => Cat-4
[1] => Item-4
)
)
[Page-3] => Array
(
[0] => Array
(
[0] => Cat-5
[1] => Item-5
)
)
[Page-4] => Array
(
[0] => Array
(
[0] => Cat-6
[1] => Item-6
)
)
[Page-5] => Array
(
[0] => Array
(
[0] => Cat-7
[1] => Item-7
)
[1] => Array
(
[0] => Cat-9
[1] => Item-9
)
)
[Page-6] => Array
(
[0] => Array
(
[0] => Cat-8
[1] => Item-8
)
)
)
Where, the first keys [Page-x] from array will be Main-Links in the navigation menu.
Some of the main links may have Sub-Links, some not.
Sub-links are the values of the key [0] of the 3rd sub-array.
And finally the URL for each and every link will be the value of key [1] of the 3rd sub-Array.
Only Pages that have more than one category will show its categories as sub-links
The navigation bar i would like to have:
1. Page-1
2. Page-2
Cat-2
Cat-3
Cat-4
3. Page-3
4. Page-4
5. Page-5
Cat-7
Cat-9
6. Page-6
the PHP code
$records = $p->main_links();
foreach ($records as $key => $value) {
$return[$value['page']][] = array($value['child'], $value['item']);
}
foreach ($return as $key2 => $value2) {
$count = 0;
/* Select a specific value within the Array */
$main_links = $value2[$count][1]; /* URL of the main Pages */
$count = count($return[$key2]);
if($count > 1) {
foreach ($value2 as $key3 => $value3)
{
$link_name = $value3[0]; /* Child Link Names */
$link_url = $value3[1]; /* URL of Child Links */
/* addedd htmlspecialchars() function to $variables that will be echoed into HTML. It provides some XSS protection */
$cat_link .= '<li>'.htmlspecialchars($link_name).'</li>';
}
$result .= '
<li '.htmlspecialchars($li_class).'><span>'.htmlspecialchars($key2).'</span>
<ul>
'.$cat_link.'
</ul>
</li>';
}else {
$result .= '
<li><span>'.htmlspecialchars($key2).'</span></li>';
}
}
Unfortunately i can't get it work... the output is not what i am expecting :(
current Output (wrong one):
1. Page-1
2. Page-2
Cat-2
Cat-3
Cat-4
3. Page-3
4. Page-4
5. Page-5
Cat-2
Cat-3
Cat-4
Cat-7
Cat-9
6. Page-6
Any help would be appreciated!
Your current code is close to working. This line will always produce a count of 1.
$count = count($value);
What you're looking for there, I believe, is:
$count = count($return[$key]);
I've found another way around which is way better than the one i was trying to do. This solved my case.
http://wizardinternetsolutions.com/articles/web-programming/single-query-dynamic-multi-level-menu
Thank you for your support!
I've been searching around quite a bit for an answer for this, but I'm afraid that I've been unable to figure out a solution to this problem. I've created a multidimensional array which includes zip code information. However, I've been unable to pull the values out of it in the way that I need to. Here's an example of the print_r():
Array (
[0] => Array (
[0] => 59101
[1] => 0.0 )
[1] => Array (
[0] => 59102
[1] => 5.0 )
[2] => Array (
[0] => 59105
[1] => 6.8 )
[3] => Array (
[0] => 59106
[1] => 9.2 )
[4] => Array (
[0] => 59037
[1] => 12.7 )
[5] => Array (
[0] => 59044
[1] => 13.9 )
[6] => Array (
[0] => 59002
[1] => 16.6 )
[7] => Array (
[0] => 59079
[1] => 19.3 )
)
I need to look through the array for a specific zip code, and then get distance (the second value in each array) associated with that zip code. I'd considered restructuring the array, but I'm unsure of how to accomplish it. Here's my current code:
EDIT## sorry, I may not have been clear. The below code is what I'm using to build the array, not to extract information from the array. I have not idea how to get the information out of the array.
$rArray = array();
foreach ($points as $point){
$zips = $point->Postcode;
$dists = number_format($point->D,1);
array_push($rArray,array($zips,$dists));
}
Any thoughts on the best way to accomplish this? Thanks!
This?
EDIT: After your question update.
function getDistanceByZip($zip) {
$array = //your array here;
foreach($array as $value) {
if($zip == $value[0]) {
return $value[1];
}
}
return false;
}
Maybe this?
foreach ($points as $point){
if ($point->Postcode === $codeIamLookingFor) {
echo "Distance: " . number_format($point->D, 1);
}
}
function arrayseek($array, $zip){
foreach($array as $k => $v){
if($v[0] == $zip){
return $v[1];
}
}
return false;
}
I'm trying to experiment with array_splice and I get an output like this (from $match)
Array
(
[Keep me Updated] => Array
(
[winner] => winnerl.jpg
[0] => value0.jpg
)
[0] => valuel.jpg //this should really be inside [Leep me Updated] array
[1] => value2.jpg //this should really be inside [Leep me Updated] array
[2] => value3.jpg //this should really be inside [Leep me Updated] array
}
from (this foreach creates puts in the values into $match)
foreach($data as $d)
{
if (isset($match[$d['data']['name']])) {
$match_loser = array($d['loser']['lrg_img']);
array_splice($match,1,0,$match_loser);
}else{
$match[$d['data']['name']] = array("winner"=>$d['winner']['lrg_img'],
$d['loser']['lrg_img']);
}
}
What I'm trying to get is bring [0],[1],[2] into the [Keep me Updated] $match array:
Array
(
[Keep me Updated] => Array
(
[winner] => winnerl.jpg
[0] => value0.jpg
[1] => value1.jpg // old one: [0] => valuel.jpg
[2] => value2.jpg // old one: [1] => value2.jpg
[3] => value3.jpg // old one: [2] => value3.jpg
)
}
This is what $data looks like
$data[] = array(
"data"=>array
(
"name"=>$name,
),
"winner"=>array
(
"lrg_img"=>$img_url_winner
),
"loser"=>array
(
"lrg_img"=>$img_url_loser
)
$data has array values, and $match is where I'm trying to sort the data. So if my values match, it'll consolidate.
Thanks!
Use the inner array as the argument to array_splice
foreach($data as $d)
{
if (isset($match[$d['data']['name']])) {
$match_loser = array($d['loser']['lrg_img']);
array_splice($match[$d['data']['name']],1,0,$match_loser);
}else{
$match[$d['data']['name']] = array("winner"=>$d['winner']['lrg_img'],
$d['loser']['lrg_img']);
}
}