I am rendering a multidimensional array obtained by a WS, it works perfectly but when changing the structure of the array to multidimensional it shows me an error when rendering. This is my array:
array(5) { ["#attributes"]=> array(1) { ["id"]=> string(13) "08" } ["nom-afi"]=> string(23) "Perez Pedro" ["PRESTAMO"]=> array(2) { [0]=> array(11) { ["num-pr"]=> string(14) "76" ["fec-dsb"]=> string(8) "20180503" ["fec-vnc"]=> string(8) "20230522" ["mto-otor"]=> string(5) "55400" ["plazo"]=> string(20) "60 MESES (60 CUOTAS)" ["tasa"]=> string(1) "0" ["cta-pgadas"]=> string(1) "3" ["cta-mora"]=> string(1) "0" ["sdo-vnc"]=> string(1) "0" ["sdo-actual"]=> string(7) "54382.9" ["cuota-niv"]=> string(7) "1177.09" } [1]=> array(11) { ["num-pr"]=> string(14) "061" ["fec-dsb"]=> string(8) "20180409" ["fec-vnc"]=> string(8) "20230522" ["mto-otor"]=> string(9) "116309.24" ["plazo"]=> string(20) "60 MESES (60 CUOTAS)" ["tasa"]=> string(1) "0" ["cta-pgadas"]=> string(1) "3" ["cta-mora"]=> string(1) "0" ["sdo-vnc"]=> string(1) "0" ["sdo-actual"]=> string(9) "114173.92" ["cuota-niv"]=> string(7) "2471.23" } } }
this is how I render
<?php foreach($PRESTAMOS_LISTADO['PRESTAMO'] as $result): ?>
<option value="<?php echo $result['num-pr']; ?>"><?php echo $result['num-pr']; ?></option>
<?php endforeach; ?>
up to this point it works quite well, until I render this other array:
array(5) { ["#attributes"]=> array(1) { ["id"]=> string(13) "17" } ["nom-afi"]=> string(23) "ALBA" ["PRESTAMO"]=> array(11) { ["num-pr"]=> string(14) "28" ["fec-dsb"]=> string(8) "20180214" ["fec-vnc"]=> string(8) "20190927" ["mto-otor"]=> string(5) "15000" ["plazo"]=> string(20) "18 MESES (18 CUOTAS)" ["tasa"]=> string(1) "0" ["cta-pgadas"]=> string(1) "6" ["cta-mora"]=> string(1) "0" ["sdo-vnc"]=> string(1) "0" ["sdo-actual"]=> string(8) "11058.57" ["cuota-niv"]=> string(6) "900.86" } }
this gave me the next code error:
Warning: Illegal string offset 'num-pr' in C:\xampp\htdocs\proyect\assets\WSHelper.php on line 90
How could I render and apply for multidimensional and unidimensional arrays? Thank you
You could use something like this to check if $PRESTAMOS_LISTADO['PRESTAMO'] is a simple result array and if so, convert it into an array of 1 result:
if (isset($PRESTAMOS_LISTADO['PRESTAMO']['num-pr'])) {
$results = array($PRESTAMOS_LISTADO['PRESTAMO']);
}
else {
$results = $PRESTAMOS_LISTADO['PRESTAMO'];
}
Then you can use $results in your foreach:
<?php foreach ($results as $result): ?>
Related
I'm working on algorithm to display my events on the website.
I want to sort my multidimensional array by specific key value.
My array:
["2022-02-28"]=>
array(1) {
[0]=>
array(3) {
["post_id"]=>
string(4) "3656"
["time"]=>
string(5) "16:05"
["priority"]=>
string(1) "0"
}
}
["2022-03-01"]=>
array(2) {
[2]=>
array(3) {
["post_id"]=>
string(4) "3656"
["time"]=>
string(5) "16:05"
["priority"]=>
string(1) "0"
}
[3]=>
array(3) {
["post_id"]=>
string(4) "3784"
["time"]=>
string(5) "13:00"
["priority"]=>
string(1) "0"
}
}
["2022-03-03"]=>
array(1) {
[5]=>
array(3) {
["post_id"]=>
string(4) "3663"
["time"]=>
string(5) "13:06"
["priority"]=>
string(1) "1"
}
}
}
I want to sort the array by "time" key value. So for example at this index :
["2022-03-01"]=>
array(2) {
[2]=>
array(3) {
["post_id"]=>
string(4) "3656"
["time"]=>
string(5) "16:05"
["priority"]=>
string(1) "0"
}
[3]=>
array(3) {
["post_id"]=>
string(4) "3784"
["time"]=>
string(5) "13:00"
["priority"]=>
string(1) "0"
}
}
I want first 13:00 to appear then 16:05. Thank you for your help in advance! :)
Try with this:
<?php
$arr = array();
$arr["2022-02-28"] = [
array("post_id"=>"3656", "time"=>"16:05", "priority"=>"0"),
array("post_id"=>"4856", "time"=>"13:05", "priority"=>"3")];
$arr["2022-03-01"] = [
array("post_id"=>"3656", "time"=>"16:05", "priority"=>"0"),
array("post_id"=>"3636", "time"=>"13:05", "priority"=>"1")
];
foreach($arr as $key => $value){
usort($value, function($a,$b){
return strtotime($a["time"])>strtotime($b["time"]);
});
$arr[$key] = $value;
}
echo "<pre>";
var_dump($arr);
echo "</pre>";
Output:
array(2) {
["2022-02-28"]=>
array(2) {
[0]=>
array(3) {
["post_id"]=>
string(4) "4856"
["time"]=>
string(5) "13:05"
["priority"]=>
string(1) "3"
}
[1]=>
array(3) {
["post_id"]=>
string(4) "3656"
["time"]=>
string(5) "16:05"
["priority"]=>
string(1) "0"
}
}
["2022-03-01"]=>
array(2) {
[0]=>
array(3) {
["post_id"]=>
string(4) "3636"
["time"]=>
string(5) "13:05"
["priority"]=>
string(1) "1"
}
[1]=>
array(3) {
["post_id"]=>
string(4) "3656"
["time"]=>
string(5) "16:05"
["priority"]=>
string(1) "0"
}
}
Use usort for define custom sort.
function time_sort(array $arr){
usort($arr, function($a, $b){
return strcmp($a['time'], $b['time']);
});
}
array(2) {
[0]=>
object(stdClass)#21 (7) {
["id"]=>
string(1) "1"
["title"]=>
string(7) "cvxzcvd"
["con"]=>
string(10) "gvsdvgsdfg"
["is_important"]=>
string(1) "1"
["date"]=>
string(3) "123"
["image"]=>
string(2) "55"
["cat_id"]=>
string(1) "1"
}
[1]=>
object(stdClass)#22 (7) {
["id"]=>
string(1) "2"
["title"]=>
string(4) "fsdf"
["con"]=>
string(9) "dfdsfvfds"
["is_important"]=>
string(1) "1"
["date"]=>
string(4) "5145"
["image"]=>
string(7) "5454124"
["cat_id"]=>
string(1) "2"
}
}
I passed this array into a view
$this->load->view('home/index',$news_data);
but I wanna use the data separately.
I mean if I wanna use the second title
or the second data.
how can I express that in the view
thanks
You can go like this:-
$news_data[1]->title;
$news_data[1]->data;
Store the array into variable like
$news_data['arr'] = { } ;
,and in view file access it by $arr;
print_r($arr);
I would like the code below to display only one time each $row[2] element (no duplicates) :
foreach($rows as $row){
echo " {$row[2]} ";
}
How can I achieve this ? Thanks.
My array is very big but here is a sample from var_dump
[0]=>
array(10) {
[0]=> string(2) "39"
["id"]=> string(2) "39"
[1]=> string(3) "abc"
["A"]=> string(3) "abc"
[2]=> string(2) "123"
["B"]=> string(2) "123"
[3]=> string(1) "0"
["C"]=> string(1) "0"
[4]=> string(1) "1"
["D"]=> string(1) "1"
}
I'm only interested about [2]=> string(2) "123".
Here is the code you can use:
$uniqueArr = array();
foreach ($rows as $row) {
if(!(in_array($row[2], $uniqueArr))) {
echo $row[2];
$uniqueArr[] = $row[2];
}
}
I am having a hard time extracting a value from the following JSON object
array(3) { [0]=> object(stdClass)#1 (11) { ["Group"]=> string(2) "18" ["GroupName"]=> string(8) "Wireline" ["Region"]=> string(2) "15" ["RegionName"]=> string(8) "Atlantic" ["Province"]=> string(1) "1" ["ProvinceName"]=> string(13) "New Brunswick" ["City"]=> string(2) "11" ["CityName"]=> string(10) "Campbelton" ["Site"]=> string(2) "37" ["SiteName"]=> string(16) "Campbellton PNCT" ["Year"]=> string(4) "2016" }
[1]=> object(stdClass)#2 (5) { ["PlatformID"]=> string(1) "1" ["PlatformTag"]=> string(6) "Access" ["Rack"]=> string(24) "23" Width 36" Depth Rack" ["RackValue"]=> string(1) "2" ["Comments"]=> string(0) "" }
[2]=> object(stdClass)#3 (12) { ["Rack"]=> string(31) "23" Width 36" Depth Rack Access" ["RackValue"]=> string(1) "2" ["RackComments"]=> string(0) "" ["Manufacturer"]=> string(6) "werwer" ["Name"]=> string(6) "werwer" ["Quantity"]=> string(1) "1" ["RackUnits"]=> string(1) "1" ["Power"]=> string(1) "1" ["ActivePassive"]=> string(6) "Active" ["ACDC"]=> string(2) "AC" ["ConnectivityIDs"]=> array(1) { [0]=> string(1) "2" } ["Connectivity"]=> array(1) { [0]=> string(5) "Fiber" } } }
I am trying to extract each item in a foreach loop within PHP Above is the var_dump of the $data[0] JSON object it demonstrates what the Array item looks like.
My foreach is the following
$data = json_decode($_POST["submitdata"]);
$Forecasts = $data[0];
foreach($Forecasts as $Forecast){
echo($Forecast->PlatformID);}
but it returns nothing as a result. Can someone explain to me how to get this from the second Array in the object?
simply place the Index of the inner array along with the object as shown below. This will check for the sub item for the PlatformID and return it to the screen.
foreach($Forecasts as $Forecast){
echo($Forecast[1]->PlatformID);}
What would be my best option to get the data out of this array?
array(4) {
[0]=> array(10) {
["id"]=> string(3) "158"
["name"]=> string(8) "Tractors"
["parent_id"]=> string(1) "0"
["image_id"]=> string(2) "37"
["blurb"]=> string(17) "Agrilife Tractors"
["brand_name"]=> string(4) "SAME"
["brand_id"]=> string(1) "2"
["cat_id"]=> string(1) "1"
["sorder"]=> string(1) "0"
["state"]=> string(1) "1"
}
[1]=> array(10) {
["id"]=> string(3) "159"
["name"]=> string(8) "Ride Ons"
["parent_id"]=> string(1) "0"
["image_id"]=> string(2) "74"
["blurb"]=> string(0) ""
["brand_name"]=> string(4) "SAME"
["brand_id"]=> string(1) "2"
["cat_id"]=> string(1) "2"
["sorder"]=> string(1) "1"
["state"]=> string(1) "1"
}
[2]=> array(10) {
["id"]=> string(3) "160"
["name"]=> string(9) "Machinery"
["parent_id"]=> string(1) "0"
["image_id"]=> string(2) "14"
["blurb"]=> string(0) ""
["brand_name"]=> string(4) "SAME"
["brand_id"]=> string(1) "2"
["cat_id"]=> string(1) "3"
["sorder"]=> string(1) "2"
["state"]=> string(1) "1"
}
[3]=> array(10) {
["id"]=> string(3) "161"
["name"]=> string(17) "Outdoor Equipment"
["parent_id"]=> string(1) "0"
["image_id"]=> string(3) "114"
["blurb"]=> NULL
["brand_name"]=> string(4) "SAME"
["brand_id"]=> string(1) "2"
["cat_id"]=> string(1) "5"
["sorder"]=> string(1) "3"
["state"]=> string(1) "1"
}
}
Tractors
My HTML looks like this I am trying to foreach to get all of the relevant data out so I can echo when or where needed.
HTML:
foreach($assoc_categories as $assoc_cat)
{
// Page load - does assoc exist?
$checked_state = "";
$does_assoc_exist = $this->Ps_products_model->brand_specific_cat_assoc_exist($brand_id, $assoc_cat['id']);
if($does_assoc_exist == "1")
{
$checked_state = " checked='checked'";
}
?>
<div>
<input type="checkbox" name="product_category" class="product_category_selector" id="product_category_<?php echo $assoc_cat['id']; ?>" data-id="<?php echo $assoc_cat['id']; ?>" <?php echo $checked_state; ?> /> <?php echo $assoc_cat['name']; ?>
</div>
<input class="order" type="input" />
<?php
}
?>
To dump all the values, you'd need nested foreach like this:
foreach ($original_array as $sub_array) {
foreach ($sub_array as $key=>$value) {
echo $key.' '.$value.'<br>';
}
}
To get just one value, you need to access it with its address. It may not be set, so check first:
foreach ($original_array as $sub_array) {
// Say you want all the `name`s
if (isset($sub_array['name'])) {
echo $sub_array['name'].'<br>';
}
}