I have multidimensional array which looks like this:
Array
(
[0] => Array
(
[banner_link] => http://www.eclipse-pos.com/eclipse/images/small_banner2.jpg
)
[1] => Array
(
[banners] => 143038313801.jpg,143038313809.jpg,143038313811.jpg
)
[2] => Array
(
[banners] => 143038306301.jpg,143038306302.jpg,143038306303.jpeg,143038306310.jpg,143038306311.jpg,143038306312.png
)
)
Now I want to rename banner_link key to banners.
Array
(
[0] => Array
(
[banners] => http://www.eclipse-pos.com/eclipse/images/small_banner2.jpg
)
[1] => Array
(
[banners] => 143038313801.jpg,143038313809.jpg,143038313811.jpg
)
[2] => Array
(
[banners] => 143038306301.jpg,143038306302.jpg,143038306303.jpeg,143038306310.jpg,143038306311.jpg,143038306312.png
)
)
I have tried with bellow code but no luck.
foreach ( $getBefamousHomepage as $k=>$v )
{
$getBefamousHomepage[$k] ['banners'] = $getBefamousHomepage[$k] ['banner_link'];
unset($getBefamousHomepage[$k]['banner_link']);
}
Getting this errors:
Notice: Undefined index: banner_link in /var/www/sitename/application/views/_templates/global_footer.php on line 14
Notice: Undefined index: banner_link in /var/www/sitename/application/views/_templates/global_footer.php on line 14
Array
(
[0] => Array
(
[banners] => http://www.eclipse-pos.com/eclipse/images/small_banner2.jpg
)
[1] => Array
(
[banners] =>
)
[2] => Array
(
[banners] =>
)
)
What I am doing wrong any idea?
Thanks.
banner_link doesn't always exist. Use isset() to see if it exists
:
if(isset($getBefamousHomepage[$k]['banner_link'])) {
$getBefamousHomepage[$k]['banners'] = $getBefamousHomepage[$k]['banner_link'];
unset($getBefamousHomepage[$k]['banner_link']);
}
You should check if banner_link exists.
And to avoid overwriting banner you should check if banner not already exists.
foreach ( $getBefamousHomepage as $k=>$v ) {
if(isset($getBefamousHomepage[$k]['banner_link']) && !isset($getBefamousHomepage[$k]['banners'])) {
$getBefamousHomepage[$k]['banners'] = $getBefamousHomepage[$k]['banner_link'];
unset($getBefamousHomepage[$k]['banner_link']);
}
}
Your code is almost good. You just need to add an extra check to see if banner_link exists.
When it does not, it throws a notice (like you see), but the code will still execute, so the "banner" index will be set to null.
Code example:
foreach ( $getBefamousHomepage as $k=>$v )
{
if (isset($getBefamousHomepage[$k]['banner_link']))
{
$getBefamousHomepage[$k] ['banners'] = $getBefamousHomepage[$k] ['banner_link'];
unset($getBefamousHomepage[$k]['banner_link']);
}
}
Some element of your array don't have banner_link and it is causing the warning when you try to access it. Try this instead:
if(isset($getBefamousHomepage[$k] ['banner_link'])) {
$getBefamousHomepage[$k] = array("banners" => $getBefamousHomepage[$k] ['banner_link']);
}
Related
I have form (display.php) that will get multiple selected option from user. Then this selected option will be formatted to another page (page.php). The problem occur when I try to display those multiple selected option. The array index are changing into string [name]!
Array ( [0] => 3204120006 [1] => 3204120011 [2] => 3204120010 [3] => 3204120009 )
Array ( [name] => BIRU ) Array ( [name] => BOJONG ) Array ( [name] => MAJAKERTA ) Array ( [name] => MAJALAYA )
Here the code of above display.
<?php
if (isset($_POST["desas"])) {
$ddes=$_POST["desas"];
print_r ($ddes);
foreach ( $ddes as $iddesa ) {
$namadesa=mysql_query("SELECT name FROM villages WHERE id='$iddesa' ");
if ($namadesa) {
$datadesa = mysql_fetch_assoc($namadesa);
print_r($datadesa);
}
} else
$datadesa="";
}
?>
My question is how to change ([name]=>BIRU),([name]=>BOJONG) into index ([0]=>BIRU),([1]=>BOJONG) etc on those array? or something missing in mysql fetch?
you can use array_values. reference: https://secure.php.net/manual/en/function.array-values.php
$datadesa = array_values(array_values);
I have the following array, since i converted the string i got back from a SOAP call to an array:
Array
(
[soapenvBody] => Array
(
[queryRequestsResponse] => Array
(
[result] => Array
(
[0] => Array
(
[BCRcustomId] => REQ16569
[BCRexternalId] => Array
(
)
[BCRrecordId] => a035700001CM60kAAD
[BCRrequestId] => a1J5700000857EYEAY
)
[1] => Array
(
[BCRcustomId] => SRQ100784
[BCRexternalId] => Array
(
)
[BCRrecordId] => a033E000001PxfAQAS
[BCRrequestId] => a1J3E0000000GSaUAM
)
)
)
)
)
I am trying to retrieve the BCRrecordId, since I need that item to make another SOAP call. I tried the following
function getID($array) {
return $array['BCRcustomId'];
}
//
$arr = array_map('getID', $array );
print_r($arr);
Now i get an error back on this saying it doesnt find it.
Undefined index: BCRcustomId in
index.php on line 97
[soapenvBody] => )Array (
My assumption is that it doenst go lower than 1 level in the array. Now i am not familair with these kinds of arrays, how would I solve this? By multiple for each loops? Or is there another way to retrieve these items
If $array is the whole response, you need to pass only result part of it:
$arr = array_map('getID', $array['soapenvBody']['queryRequestsResponse']['result']);
I have an array like them i would like to access this sub category array like this
foreach($data as $parent_category){
$ndata=$parent_category['subCategory'];
foreach ($ndata as $subCategory){
}
}
Where $data is my main array print_r($data) give this output
When i access this array i got an error Undefined index: subCategory
Help me please ...
Array (
[1] => Array
(
[name] => Indian Culture
[subCategory] => Array
(
[0] => Array
(
[name] => Indain Culture-1
[articleId] => 10
)
[1] => Array
(
[name] => culture -1
[articleId] => 22
)
)
)
[5] => Array
(
[name] => ABC CULTURE
)
)
As You see, here:
[5] => Array
(
[name] => ABC CULTURE
)
Your array does not contain element with index "subCategory". So just check, that the index is present by invoking:
...
if (isset($parent_category['subCategory'])) {
...
Array key 5 doesn't have a subCategory index.
The first entry in the array will be fine.
You can check if subCategory is present using the array_key_exists function.
http://uk1.php.net/array_key_exists
Difference between isset and array_key_exists
Item with key 5 does not contain subCategory key. To avoid warnings, try with:
foreach($data as $parent_category){
if (isset($parent_category['subCategory'])) {
$ndata = $parent_category['subCategory'];
foreach ($ndata as $subCategory){
}
}
}
Modified your code as below, so you will not get undefined index for those cases where you didn't have subcategory.
foreach($data as $parent_category){
$ndata=isset($parent_category['subCategory'])?$parent_category['subCategory']:'';
if(!empty($ndata)){
foreach ($ndata as $subCategory){
}
}
}
I use a library for Synchronize a local WebSQL DB to a server specifically https://github.com/orbitaloop/WebSqlSync.
I use PHP: 5.4.7,
When I try to get the array values as follows, I get the message
Illegal string offset 'clientes'
the $obj var is:
Array
(
[info] =>
[data] => Array
(
[clientes] => Array
(
)
[conceptos_gastos] => Array
(
)
[formaspago] => Array
(
[0] => Array
(
[idFormaPago] => 10
[FormaPago] => qwerqwe
[Dias] => 1
[Cuotas] => 1
[last_sync_date] =>
)
)
[listaprecios] => Array
(
)
[producto] => Array
(
)
[repartidores] => Array
(
)
[tipodocumento] => Array
(
)
[vehiculos] => Array
(
)
[zonas] => Array
(
)
)
)
this is the loop
foreach ($obj as $row => $value) {
echo $row["clientes"]["fomaspago"]["FormaPago"];
}
eternally grateful for any help
it seems to be
$row["data"]["clientes"] // which is an empty array
or
$row["data"]["formaspago"][0]["FormaPago"] // which should output "qwerqwe"
The element $row["clientes"]["fomaspago"]["FormaPago"]; does indeed not exist - look at the output: the 1st row "info" does not have that index, the 2nd row "data" has "clientes" and also "fomasgapo", but does not have a "clientes" "fomasgapo".
You need to either structure your data differently or loop through it differently...
thanks to all but the only way that worked was as follows:
foreach($obj->data->formaspago as $formaspago) {
print " id ".$formaspago->idFormaPago;
print " Formapago ".$formaspago->FormaPago;
print " dias ".$formaspago->Dias;
print " cuotas ".$formaspago->Cuotas;
print " lastsyncdate ".$formaspago->last_sync_date;
}
foreach($obj->data->clientes as $formaspago) {
print " id ".$formaspago->IdCliente;
print " Cliente ".$formaspago->Cliente;
}
This is my code,
foreach ( $tick_data as $tick_index => $tick_entries ) {
foreach ( $base_data as $baseproject ) {
if ( $baseproject['name'] == $tick_entries['project_name'] )
$projects_id_tickcamp[$baseproject['id']]['tick_id'] = $tick_entries['project_id'];
}
}
which gives multidimensional array:
Array
(
[1181543] => Array
(
[tick_id] => 445196
)
[1287834] => Array
(
[tick_id] => 437574
)
)
and in this code, after apply post message function, i am getting different array named message id created an sub array:
foreach ( $projects_id_tickcamp as $basecamp_project_id => $tick_id ) {
$post_message_id = $this->post_message( $basecamp_project_id );
$projects_id_tickcamp[$basecamp_project_id]['message_id'] = $post_message_id;
}
Output of this code:
Array
(
[1181543] => Array
(
[tick_id] => 445196
[message_id] => 5208908
)
[1287834] => Array
(
[tick_id] => 437574
[message_id] => 5208912
)
)
Problem is if i am getting more $baseproject['id'] in future then i have to post message on that $baseproject['id'] except on the whole array again. Please have a look on this question, any suggestion would be grateful to me.