For getting the unique values i am using unique values. Below is the code i am using
array_unique($results_external);
aasort($results_external,"created_on");
$returns_buy_external[]=array_reverse($results_external, true);
If i use the code like this, below is the error i am getting
A PHP Error was encountered Severity: Notice
Message: Array to string conversion
Filename: models/product_model.php
Line Number: 3550
3550 line is array_unique($results_external);
Can anyone help me, why it is getting error like this and how to solve it?
results_external sample format coming is below
Array
(
[0] => Array
(
[id] => 144
[name] => test
[slug] => test
[images] => {"9eebd0f69772dd3bdf8c787864437c85":{"filename":"9eebd0f69772dd3bdf8c787864437c85.png","alt":"TRESemme Smooth and Shine","caption":""}}
[track_stock] => 1
[seo_title] => ttt
[qty] => 0
[product_type] => 0
[price] => 0.00
[saleprice] => 0.00
[external_links] => http://test.com
[external_price] => 285.00
[external_saleprice] => 285.00
[created_on] => 2013-11-08 15:03:24
)
)
As per the docs, array_unique compares elements as strings by default. This means your 2D array is being converted to an array of strings (all being "Array" and generating the array-to-string Notice) or which only one can be returned as unique.
Use the SORT_REGULAR flag to compare the elements as they are, but be aware that arrays are only considered equal if they have the same key-value pairs.
Example:
print_r(array_unique($array, SORT_REGULAR));
Related
i want display the backdrop_path of the movie but i get this error
Notice: Undefined index: backdrop_path in /..../public_html/movie.php on line 167
I have added
print_r($tmdbResult);
to see the content of $tmdbResult. I want to display just backdrop_path.
Array
(
[movie_results] => Array
(
[0] => Array
(
[adult] =>
[backdrop_path] => /mbA7SCtJoFTactP1lDHA055qCf.jpg
[genre_ids] => Array
(
[0] => 35
[1] => 28
)
[id] => 261392
[original_language] => en
[original_title] => American Ultra
[overview] => A stoner and his girlfriend's sleepy, small-town existence is disrupted when his past comes back to haunt him in the form of a government operation set to wipe him out.
[release_date] => 2015-08-21
[poster_path] => /6oGHH27nqaLGfpcgYRIZYSJs7AD.jpg
[popularity] => 3.509263
[title] => American Ultra
[video] =>
[vote_average] => 5.6
[vote_count] => 134
)
)
[person_results] => Array
(
)
[tv_results] => Array
(
)
[tv_episode_results] => Array
(
)
[tv_season_results] => Array
(
)
)
The code i use is
ini_set("display_errors",1);
$tmdbResponse = curl_exec($ch);
curl_close($ch);
$tmdbResult = json_decode($tmdbResponse, true );
$backdrop_path = $tmdbResult['movie_results']['backdrop_path'];
$smarty->assign("backdrop_path",$backdrop_path);
print_r($tmdbResult);
The reason is you're getting a numeric index, because the result can contain multiple movies. In this case you're getting just one.
All you need to do is access the array like this #u_mulder said:
$bp = $tmdbResult['movie_results'][0]['backdrop_path'];
The [0] meaning that in case you get multiple results, just need to change that index to access the others. May be you shoulds think of a foreach loop, unless for some reason of yours, you KNOW you'll always get ONE single movie, in that case, hardcode your $bp = $tmdbResult['movie_results'][0]['backdrop_path']; no problem.
I am analyzing someone's else code where I found during debugging the type and values of a class variable during run time:
echo print_r($this->_out);
Array
(
[id] => -1
[fieldErrors] => Array
(
)
[error] =>
[data] => Array
(
)
[row] => Array
(
[DT_RowId] => row_177
[id] => 177
[last_name] => sdfdsf
[first_name] => dsf
[homeaddr] => sdfdsfsdfdsfdsfdsfdsf
[email] => s#jj.com
[officeaddr] => wwwwwwwwwwwwwwwwwwwwwwww
[mobile] => 11111111
[age] => 11
[chargeamt] => 11
[start_date] => 11/11/2011
)
)
1{"row":{"DT_RowId":"row_177","id":"177","last_name":"sdfdsf","first_name":"dsf","homeaddr":"sdfdsfsdfdsfdsfdsfdsf","email":"s#jj.com","officeaddr":"wwwwwwwwwwwwwwwwwwwwwwww","mobile":"11111111","age":"11","chargeamt":"11","start_date":"11\/11\/2011"}}
I am a newbie in PHP and would like to know how I can access [id] => 177 value i.e. value 177.
I tried out many ways
$this->_out['row']['id'][0]
It gave me below result:
1{"row":{"DT_RowId":"row_177","id":"177","last_name":"sssss","first_name":"ss","homeaddr":"sssssssssssssssssssss","email":"ss#ww.com","officeaddr":"sssssssssssssssssssssssssssss","mobile":"11111111","age":"11","chargeamt":"11","start_date":"01\/01\/2001"}}
while
I tried out many ways
$this->_out['row']['id']
It gave me below result:
177{"row":{"DT_RowId":"row_177","id":"177","last_name":"sssss","first_name":"ss","homeaddr":"sssssssssssssssssssss","email":"ss#ww.com","officeaddr":"sssssssssssssssssssssssssssss","mobile":"11111111","age":"11","chargeamt":"11","start_date":"01\/01\/2001"}}
and others but its just not giving me the expected.
How can I access the value as desired?
You are doing it right. $this->_out['row']['id'] will return desired result (check why you get also JSON string that is not part of print_t($this->_out).
This will return result 177:
$this->_out['row']['id'];
And since in PHP you can access string characters as array, this will returns first character in string (that is 1):
$this->_out['row']['id'][0];
And this will throw error as there is no such index (string length is 3, so last index is 2):
$this->_out['row']['id'][5];
The print_r result is an array with more arrays on it. So first of all you must find the index of the main array which index represents the sub-array with the value you are looking for. And then you must use this index to access the sub array values.
I'm trying to access a piece of data in an array of arrays that (I believe) is in an object (this may not be the right term though).
When I do print_r on this: $order_total_modules->process() I get...
Array (
[0] => Array (
[code] => ot_subtotal
[title] => Sub-Total:
[text] => $49.99
[value] => 49.99
[sort_order] => 1
)
[1] => Array (
[code] => ot_total
[title] => Total:
[text] => $0.00
[value] => 0
[sort_order] => 12
)
)
If I run echo $order_total_modules->process()[1][3];, I should get "0", because that is the 3rd element of the 2nd array... right? Yet, I get an error.
Can anyone help with this?
Even though it is the third element counting from 0, the index is not 3 it is an associative array with the index value:
Available in PHP >=5.4.0:
echo $order_total_modules->process()[1]['value'];
Or PHP < 5.4.0:
$result = $order_total_modules->process();
echo $result[1]['value'];
You cannot access an associative array via an integer index(unless the index is an actial integer).
So in this case use :
[1]['code'] to access what woulde be [1][0] with a 'normal' array.
Try putting it in a var first:
$ar = $order_total_modules->process();
echo $ar[1]['value'];
The second level array is an assoc, which means that the key is not numeric, which means that you need to call the name of the key, hence the 'value'.
I am trying to insert an array in to MySQL using PHP. I followed the excellent advice given on here to use the implode command and it worked great for one array, but this one seems to be dying. This array is slightly different than the other, but I don't know how to explain the difference.
Here is my code:
$sql = array();
foreach( $ride_detail as $row ) {
$sql[] = '('.$row['id'].', "'.mysql_real_escape_string($row['name']).'",
"'.$row['version'].'")';
}
mysql_query('INSERT IGNORE INTO ride (ride_id, name, version) VALUES '.implode(',', $sql));
I'm getting this message over and over again.
Warning: Illegal string offset 'id' in ride_details.php on line 60
Warning: Illegal string offset 'name' in ride_details.php on line 60
Warning: Illegal string offset 'version' in ride_details.php on line 61
The content of my array (using print_r) is:
Array ( [id] => 21570117 [name] => Night ride home from work [start_date_local] => 1347302039 [elapsed_time] => 53:56 [moving_time] => 52:04 [distance] => 12.6 >>[average_speed] => 14.5 [elevation_gain] => 474 [location] => Englewood, CO [start_latlng] => Array ( [0] => 39.547792011872 [1] => -104.86300536431 ) [end_latlng] => Array ( [0] => 39.655485888943 [1] => -104.88656991161 ) [version] => 1355428869 [athlete] => Array ( >>[id] => 832001 [name] => Bob Kratchet [username] => bob_kratchet ) [bike] => Array ( [id] => 281303 [name] => Giant Allegre commuter ) [maximum_speed] => 29.3 [calories] => 372 >[average_power] => 107 [commute] => 1 )
I am a complete noob...
Since your $ride_detail is just one array, $row is 21570117 (integer), Night ride home from work (string), and so on, one by one. The code then attempts to get the id key of each element, then the name key, and so on, generating a [expletive]-ton of error messages as it goes.
It looks like you're intending to have $ride_detail be an array of arrays, or you don't actually want a foreach loop at all.
try this
foreach( $ride_detail as $row ) {
$sql = array( $row['id'], mysql_real_escape_string($row['name']), $row['version']) ;
}
mysql_query('INSERT IGNORE INTO ride (ride_id, name, version) VALUES ($sql[0] ,$sql[1] , $sql[2] ) ');
Thought I will point out first that I have looked around on Stackoverflow and the internet already and although they are plenty of examples none of them are explained into such a way for me to understand how to convert the code to work with my array structure.
I believe I need to use one of the functions uksort or uasort but unsure on this as well.
My array looks like the following.
Array
(
[0] => Array
(
[Result] => Array
(
[id] => 260
[event_id] => 72
[year] => 2010
[york_score] => 27
[york_points] => 0.0
[lancs_score] => 51
[lancs_points] => 4.0
[winner] => L
[confirmed] => Y
[updated] => 2010-05-01 16:10:03
)
[Event] => Array
(
[id] => 72
[sport_id] => 25
[event_title] => 1st Team
[Sport] => Array
(
[id] => 25
[sport_title] => Netball
)
)
)
And where its [0] means it continues on.
I need to sort all of the arrays [0,1,2,3,...] by the sport_title key found in [Event][Sport]
Does anyone know how to create a sorting function to do this?
Some explanation of how the function is working would also be helpful if I later need to adapter/alter the code to work else where on my site.
Where $array is the name of the array which holds the array you posted in your question.
function sort_multi($item_1, $item_2)
{
// strcmp looks at two strings and, based off the characters' and their order,
// determines which one is numerically greater. When this function returns a
// negative, for example, it means the first item it's comparing is less that the
// second item (ef and eg, for example). The usort function then rearranges
// the array based off these comparisons.
return strcmp($item_1['Event']['Sport']['sport_title'], $item_2['Event']['Sport']['sport_title']);
}
usort($array, 'sort_multi');