codeigniter replace array values in model - php

I have been going around in circles with a multidimensional array replace..
I need to replace numbers stored in a DB that relate to a status type.. If I did this in the view it would work but it wont seem to replace in the model?
function fetch_shipments($orgID){
$this->db->select('shipID, shipRef, shipCarrier, shipOrigin, shipDestination, shipQty, shipStatus');
$this->db->where('orgID', $orgID);
$query = $this->db->get('shipments');
$result = $query->result_array();
foreach($result as $row) {
foreach ($row as $key => $val) {
$key == 'shipStatus' && $val == 0 ? $val = 'Current' : $val;
$key == 'shipStatus' && $val == 1 ? $val = 'Pending' : $val;
$key == 'shipStatus' && $val == 2 ? $val = 'Complete' : $val;
}
}
return $result;
}
Has really left me scratching my head, I know this kind of foreach works as I use it all the time... I feel I am missing something (perhaps obvious) but I just cant put my finger on it. I even tried doing the replace at the object level before outputting an array but couldn't get that to work either.

you should save it on your $result variable. not in $val
foreach($result as $k => $row) {
foreach ($row as $key => $val) {
$key == 'shipStatus' && $val == 0 ? $result[$k][$key] = 'Current' : $val;
$key == 'shipStatus' && $val == 1 ? $result[$k][$key] = 'Pending' : $val;
$key == 'shipStatus' && $val == 2 ? $result[$k][$key] = 'Complete' : $val;
}
}
return $result;
Or you could remove the inner loop
foreach($result as $k => $row) {
if($row['shipStatus']==0){
$result[$k]['shipStatus'] = 'Current';
}elseif($row['shipStatus']==1){
$result[$k]['shipStatus'] = 'Pending';
}else{
$result[$k]['shipStatus'] = 'Complete';
}
}
return $result;

I believe placing a & before your loop variable should solve your problem, as then the reference would get updated.
So your loop would start something like this
foreach($result as &$row) {
foreach ($row as $key => &$val) {

Related

Change the array structure in php

How can i change the test1 array format as like in test2?
$test1 = array(
'size'=>array('V'=>array('V'),'R'=>array('R','R')),
'price'=>array('V'=>array('77'),'R'=>array('88','99')),
'unit'=>array('V'=>array('3'),'R'=>array('3','2')),
'color'=>array('V'=>array('Black'),'R'=>array('Green','Red')));
$test2 = array(
'size'=>array('V','R'),
'price'=>array('V'=>array('Black'=>'77'),
'R'=>array('Green'=>'88','Red'=>'99')),
'unit'=>array('V'=>array('Black'=>'3'),
'R'=>array('Green'=>'3','Red'=>'2')),
'color'=>array('V'=>array('Black'),'R'=>array('Green','Red')));
Thanks!
With a foreach loop you can change the array structure.
http://php.net/manual/de/control-structures.foreach.php
Something like that should work: its pretty unclear what the rules are to convert from one array to the other in your case.
$test2 = array();
foreach ($test as $type => $array) {
$newArray = array();
foreach ($array as $key => $value) {
if ($key == 'V') {
if ($value == 3) {
$newArray['V'] = array('Black' => 3);
} else if ($value = 2) {
$newArray['V'] = array('Green' => 2);
}
} else if ($key == 'R') {
//....
}
}
$test2[$type] = $newArray;
}
Thanks for the response. and sorry for not being more specific. Actually i wanted all the values in dynamic manner. I constructed a loop :). Thanks!
$myArray = array();
foreach($test as $type => $array){
$k = 0;
foreach ($array as $key => $value) {
if($type == 'size'){
$myArray['size'][] = $key;
}else if($type == 'price' || $type == 'unit' ){
$m = 0;
foreach($value as $vall){
if($type == 'price'){
$myArray['price'][$myArray['size'][$k]][$test['color'][$myArray['size'][$k]][$m]] = $test['price'][$myArray['size'][$k]][$m];
}else if($type == 'unit'){
$myArray['unit'][$myArray['size'][$k]][$test['color'][$myArray['size'][$k]][$m]] = $test['unit'][$myArray['size'][$k]][$m];
}
$m++;
}
}else if($type == 'color'){
$myArray['color'][] = $value;
}
$k++;
}
}

PHP, Echo only one time in a foreach loop

i'have an array, and then i have a script who gets the category i'm browsing (using wordpress) and put it in the $category variable.
So i test if the category i'm browsing it's equal to the $array key and then i paste some text
$array = array ('key' => 'value', ... )
//...
// a script who gets the category i'm browsing and store it in the $category variable
//...
/* starting the foreach loop */
foreach( $array as $key => $value) {
if ($category == $key) {
echo "some $value here";
} elseif ($category !== $key) {
echo "nothing";
}
The problem is that this loop does echo "nothing" for each time the $category is not equal to the $key for each element of the array.
So if i have 20 key => value in the array this loop paste one time "some $value here" and 19 times "nothing"
there is a way to echo "nothing" only one time?
Thank you!
You can use array_key_exists instead of the foreach loop:
if (array_key_exists($category, $array)) {
echo $array[$category];
} else {
echo 'nothing';
}
$i=0;
foreach( $array as $key => $value) {
$i++;
if ($category == $key) {
echo "some $value here";
} elseif($category !== $key)
{
if($i<=1)
{
echo "nothing";
}
else{}
}
Try with -
$i = 1;
foreach( $array as $key => $value) {
if ($category == $key) {
echo "some $value here";
} elseif ($category !== $key) {
$i++;
}
}
if (count($array) == $i) {
echo "nothing";
}

PHP: Wildcards on multidimensional key

I have a multidimensional array $array["A"]["B"]["C"]["D"]. The list is longer.
Is there a wildcard that I can use to get ["D"] value in let say ["B"] array?
Something like this, $array["A"]["B"][*]["D"] ?
or $array[*]["B"][*]["D"] ?
Example, I would like to get all prices that were bought on February regardless of the year.
$array[2013][2][23]["ItemName"]["ItemPrice"] .....
If this would work, it would be really wonderful
$array[*][2][*][*]["ItemPrice"]..
any idea?
You could do multiple foreach to loop though every nested array that you want to loop though.
foreach ($array as $a) {
foreach ($a["B"] as $c) {
foreach ($c as $d) {
// Do something with $d
}
}
}
This would be $array[*]["B"][*][*]
Edit: You could combine my suggestion with a while loop.
$innerArray = $array;
while (true) {
foreach ($array as $key => $value) {
if ($key == "D") {
// Do something with this value
} else if (is_array($value)) {
$innerArray = $value;
} else {
break;
}
}
}
Thanks to #Sepehr-Farshid it just crossed my mind that I can use recursive function (Something that I haven't use for quiet a while. So here a example.
$newarray = array();
$tempArray = $oldarray;
$levels[] = 1;
$keys[] = 2;
$levels[] = 4;
$keys[] = "ItemPrice";
$lastLevel =4;
recurArray($tempArray, 0);
function recurArray($array, $level)
{
foreach($array as $key => $value) {
if(array_search($level, $GLOBALS["levels"]) {
$tempKey = array_search($level, $GLOBALS["levels"];
if($key == $GLOBALS["keys"][$tempKey] {
if($level == $GLOBALS["lastLevel"]) $GLOBALS["newarray"] = $value;
else recurArray($value, $level + 1);
}
else { return; }
}
else { recurArray($value, $level + 1); }
}
}
this might not be the optimum way, but it will work and can be refined. :D

how to use variable outside foreach loop

How to return $value after loop with its returned data ? I think to create array before loop and equal it to $v to use it after loop but it didn't work.
Any idea on how to solve this problem ?
// create array
$v = array();
// start loop
foreach ($this->json_data->locations as $key => $value) {
if ($value->country_name == $data['city']->country_name)
// return $value with data
return $v = $value ;
}
echo $v->country_name
try this:
$v = array();
foreach ($this->json_data->locations as $key => $value) {
if ($value->country_name == $data['city']->country_name)
{
if(!in_array($value,$v))
{
array_push($v,$value);
}
}
}
try this
$v = array();
$i=0;
// start loop
foreach ($this->json_data->locations as $key => $value) {
if ($value->country_name == $data['city']->country_name)
// return $value with data
$i++;
$v[$i] = $value ;
}
//print $v
print_r($v)
If like using 'return' try this.
$v = iLikeUsingReturn($this,$data);
function iLikeUsingReturn($t,$d){
foreach ($t->json_data->locations as $key => $value) {
if ($value->country_name == $d['city']->country_name)
return $value ;
}
return array();
}
I think the following code will helps you.
// create array
$v = array();
// start loop
foreach ($this->json_data->locations as $key => $value) {
if ($value->country_name == $data['city']->country_name)
// return $value with data
array_push($v, $value);
}
return $v;

how to count strings in php?

i have this situation"
foreach($front->getRequest()->getParams() as $key => $value){
if ($value == '1'){
$$key = $value;
}
}
echo $test1; // test1 = 1
echo $test2; // test2 = 1
....
this will give me back one or more $test = 1 where the $$key = $test and $value = 1
i want to see how many actually come back. and i was thinking to do something like: print_r(count($key)) or print_r(count($value)) but it doesn't tell me how many results are there
any ideas?
thanks
Why not just keep a counter?
$count = 0;
foreach($front->getRequest()->getParams() as $key => $value){
if ($value == '1'){
$$key = $value;
$count++;
}
}
echo $count;

Categories