I get this result from web service in php code:
> stdClass Object (
> [Post_Added_Record_ByMenuIDResult] => stdClass Object
> (
> [HTMLStructure] => ثبت انجام شد
> [IsAuthenticated] => 1
> [MenuID] => 1191
> [Password] =>
> [PersonID] => 27598
> [PersonalityID] => 31413
> [RecordValues] =>
> [RoleID] => 5
> [Sexuality_Title] => m
> [UserName] => mr.piri
> ) )
I want to get the opposite phrase in [HTMLStructure]. in this example : ثبت انجام شد
if (strstr( $result, 'HTMLStructure' ) ) {
$HTMLStructurePos=strpos( $result, '[HTMLStructure] =>' );
echo $HTMLStructurePos;
$IsAuthenticatedPos=strpos( $result, ' [IsAuthenticated]' );
echo $IsAuthenticatedPos;
$result2 = substr($result, $HTMLStructurePos, $IsAuthenticatedPos);
echo "<pre>";
print_r($result2);
echo "</pre>";
} else {
echo "Not found";
}
but i get
Not found
in the out put.
I think this is a structure and I have to get the value with the pointer.But does anyone know how?
You get a nested object back from your Web-Service. Simplified as PHP code:
$result = (object)['Post_Added_Record_ByMenuIDResult' => (object)[
'HTMLStructure' => "ثبت انجام شد",
'IsAuthenticated' => 1,
//...
]];
The HTML can be accessed simply like this:
$html = $result->Post_Added_Record_ByMenuIDResult->HTMLStructure;
echo $html; // ثبت انجام شد
Just like HTMLStructure, you also get the values like IsAuthenticated.
i solved this problem by this code:
foreach ($result as $obj)
{
// Here you can access to every object value in the way that you want
$result2 = $obj->HTMLStructure;
}
Related
I am getting my query output in the model and sending to controller If I check the data in the controller like print_r($result);
I am getting the output from my Model and My output is
stdClass Object
(
[membership_id] => 11
[member_id] => 10
[customer_id] =>21
[membership_approve] => 1
[membership_status] => 1
[profile_pic] =>
[first_name] => Juvin
[middle_name] => kumar
[last_name] => choudhary
[email] => juvin#gmail.com
[dob] => 2018-07-01
[phone] => 01236541254
[is_Approved] => 1
)
Model code is
if ($result) {
foreach($result as $result_set ){
if($result_set->membership_status == 1)
{
$result2=$this->db->select('*')
->from('membership_details')
->join('members','members.member_id = membership_details.member_id')
->group_start()
->where('members.is_Approved',1)
->where('membership_details.membership_status', 1)
->where('membership_details.member_id',$result_set->member_id)
->group_end()
->get()
->row();
return $result2;
}
else
{
return 0;
}
}
}
else{echo "Not working";}
Now I am on the controller. I am passing the output in the foreach but when I check $row->phone then it's showing meMessage: Trying to get property of non-object`
$result=$this->Search_model->check_membership($customer_name,$customer_mobile);
$arr_result2 = array();
if (count($result) > 0)
{
print_r($result);// It's displaying the same output which I aaded in the question.
foreach($result as $row){
print_r($row->customer_id);// not getting customer id
$arr_result2[] = array(
"profile_pic" => $row->profile_pic,
"name" => $row->first_name.' ' .$row->last_name,
"phone" => $row->phone
);
}
}
else{echo "No data availble";}
//print_r($arr_result2);
echo json_encode($arr_result2);
Hope this will help you :
row() fetches single record so u don't need to loop it do like this :
$result = $this->Search_model->check_membership($customer_name,$customer_mobile);
$arr_result2 = array();
if (count($result) > 0)
{
print_r($result);// It's displaying the same output which I aaded in the question.
$arr_result2[] = array(
"profile_pic" => $result->profile_pic,
"name" => $result->first_name.' ' .$result->last_name,
"phone" => $result->phone
);
}
else
{
echo "No data availble";
}
//print_r($arr_result2);
echo json_encode($arr_result2);
I Connect to a remote SSH device in Php.
I run a command that output is too long.
So I searched and understood I can save it via :
$my_array= explode("\n", $this->ssh->exec('some command'));
Its ok, Now I can display whole output via :
echo print_r($my_array);
Output is something like :
Array ( [0] => ath4_auth_mode=disabled [1] => wl_mac_deny= [2] => filter_dport_grp3= [3] => ses_script= [4] => http_redirect_port=3128 [5] => oet5_en=0 [6] => filter_dport_grp4= [7] => oet2_fragment=0 [8] =>
When I run :
echo count($my_array);
It displays me :
2200
So it is true, Now I wanna search for a specific text like name=,I want the value after equal in the array, I tried this :
$search_result = array_search("name=", $my_array);
But no chance, Even tried :
foreach($my_array as $cat) {
$cat = trim($cat);
if($cat == "name=") {
echo "hoola !";
} else {
echo ':-(';
}
Again no chance, How can I search for the name= and get the value after = ?
I suppose in your array value is not name= exactly. It can be name=something for example. In this case neither == nor in_array will find it. You need to use strpos:
foreach($my_array as $cat) {
$cat = trim($cat);
if(strpos($cat, "name=") === 0) {
echo "hoola !", $cat;
// add a break if value found so as not to search other elements
break;
} else {
echo ':-(';
}
}
I used === and 0 because name= must be in the beginning of your $cat.
I'm performing a for loop to iterate through MLS listings. I'm able to get every bit of information I need but I can't seem to figure out how to extract the URL from the array I get.
Here's the array I get from my for loop:
PHRETS\Models\Object Object
(
[content_type:protected] => text/xml
[content_id:protected] => 9577056
[object_id:protected] => 1
[mime_version:protected] =>
[location:protected] => http://cdnparap100.paragonrels.com/ParagonImages/Property/P10/CAT/9577056/0/0/0/42ab28468ab0dfc6fd83dfb39e5dfff7/3/55ec2da6d4a32437d345d0992fae1851/9577056.JPG
[content_description:protected] =>
[content_sub_description:protected] =>
[content:protected] =>
[preferred:protected] =>
[error:protected] => PHRETS\Models\RETSError Object
(
[code:protected] => 0
[message:protected] =>
)
)
The following is the code I'm using to get that output. I'm just wondering if there's a way to do a trim or something to extract the URL so I can store it.
for ($i = 0; $i <= count($listID); $i++) {
$photo = $rets->GetObject('Property', 'Photo', $mls, '*', 1);
foreach ($photo as $image) {
echo "<pre>";
print_r($image);
echo "<br>";
echo "</pre>";
}
}
You mean like
foreach ($photo as $image) {
echo 'Location: ', $image->getLocation(), '<br>';
}
?
See https://github.com/troydavisson/PHRETS/blob/master/README.md#downloading-media-photos-images-documents-etc
The method is detailed here ~ https://github.com/troydavisson/PHRETS/blob/master/src/Models/Object.php#L109
help me to convert the following array in to json.
I tried to convert the array.
Array
(
[0] => Array
(
[c_code] => 200001
[itemname] => 303 10CAP
[c_pack_code] => PK0075
[c_web_img_link] =>
)
[1] => Array
(
[c_code] => 200005
[itemname] => 3P 4TAB
[c_pack_code] =>
[c_web_img_link] =>
)
)
current result for the following code is
public function searchOrder($idx, $data) {
if (!empty($data)) {
$result = OrderbukModel::func_get_searchlist($idx,$data);
if (!empty($result)) {
$resultArray[] = $result;
print_r(json_encode($result));
} else {
$resultArray[$idx] = ["Mysql returns empty result !"];
print_r(json_encode($resultArray));
exit;
}
}
}
now i got the result is like
[{"c_code":"200001","itemname":"303 10CAP","c_pack_code":"PK0075","c_web_img_link":""},{"c_code":"200005","itemname":"3P 4TAB","c_pack_code":"","c_web_img_link":""}]
But I need the result as follows
[{"c_code":"2000001","c_code":"200005"},
{"itemname":"303 10CAP","itemname":"3P 4TAB"},
{"c_pack_code":"PK0075","c_pack_code":""},
{"c_web_img_link":"","c_web_img_link":""}]
Example of how you can you make the json from array. Collect the data in two different array and after loop marge them and store the result in another array after that encode them.
Note: Your desired JSON is not a valid format, you can't use same index
for two data.
Online Example: https://3v4l.org/kdPDI
$arr = array(
array(
'c_code' => '200001',
'itemname' => '303 10CAP',
'c_pack_code' => 'PK0075',
'c_web_img_link' => ''
),
array(
'c_code' => '200005',
'itemname' => '3P 4TAB',
'c_pack_code' => '',
'c_web_img_link' => ''
)
);
$res1 = array();
$res2 = array();
foreach($arr as $val){
$res1['c_code'][] = $val['c_code'];
$res1['itemname'][] = $val['itemname'];
$res2['c_pack_code'][] = $val['c_pack_code'];
$res2['c_web_img_link'][] = $val['c_web_img_link'];
}
$out = array(array_merge($res1, $res2));
echo json_encode($out);
Is there a better way of doing this PHP code? What I'm doing is looping through the array and replacing the "title" field if it's blank.
if($result)
{
$count = 0;
foreach($result as $result_row)
{
if( !$result_row["title"] )
{
$result[$count]["title"] = "untitled";
}
$count++;
}
}
Where $result is an array with data like this:
Array
(
[0] => Array
(
[title] => sdsdsdsd
[body] => ssdsd
)
[1] => Array
(
[title] => sdssdsfds
[body] => sdsdsd
)
)
I'm not an experienced PHP developer, but I guess that the way I've proposed above isn't the most efficient?
Thanks
if($result) {
foreach($result as $index=>$result_row) {
if( !$result_row["title"] ) {
$result[$index]["title"] = "untitled";
}
}
}
You don't need to count it. It's efficient.
if ($result)
{
foreach($result as &$result_row)
{
if(!$result_row['title'])
{
$result_row['title'] = 'untitled';
}
}
}
Also, you may want to use something other than a boolean cast to check the existence of a title in case some young punk director releases a movie called 0.
You could do something like if (trim($result_row['title']) == '')
Mixing in a little more to #Luke's answer...
if($result) {
foreach($result as &$result_row) { // <--- Add & here
if($result_row['title'] == '') {
$result_row['title'] = 'untitled';
}
}
}
The key is the & before $result_row in the foreach statement. This make it a foreach by reference. Without that, the value of $result_row is a copy, not the original. Your loop will finish and do all the processing but it won't be kept.
The only way to get more efficient is to look at where the data comes from. If you're retrieving it from a database, could you potentially save each record with an "untitled" value as the default so you don't need to go back and put in the value later?
Another alternative could be json_encode + str_replace() and then json_decode():
$data = array
(
0 => array
(
'title' => '',
'body' => 'empty',
),
1 => array
(
'title' => 'set',
'body' => 'not-empty',
),
);
$data = json_encode($data); // [{"title":"","body":"empty"},{"title":"set","body":"not-empty"}]
$data = json_decode(str_replace('"title":""', '"title":"untitled"', $data), true);
As a one-liner:
$data = json_decode(str_replace('"title":""', '"title":"untitled"', json_encode($data)), true);
Output:
Array
(
[0] => Array
(
[title] => untitled
[body] => empty
)
[1] => Array
(
[title] => set
[body] => not-empty
)
)
I'm not sure if this is more efficient (I doubt it, but you can benchmark it), but at least it's a different way of doing the same and should work fine - you have to care about multi-dimensional arrays if you use the title index elsewhere thought.
Perhaps array_walk_recursive:
<?php
$myArr = array (array("title" => "sdsdsdsd", "body" => "ssdsd"),
array("title" => "", "body" => "sdsdsd") );
array_walk_recursive($myArr, "convertTitle");
var_dump($myArr);
function convertTitle(&$item, $key) {
if ($key=='title' && empty($item)) {$item = "untitled";}
}
?>
If you want sweet and short, try this one
$result = array(
array(
'title' => 'foo',
'body' => 'bar'
),
array(
'body' => 'baz'
),
array(
'body' => 'qux'
),
);
foreach($result as &$entry) if (empty($entry['title'])) {
$entry['title'] = 'no val';
}
var_dump($records);
the empty() will do the job, see the doc http://www.php.net/manual/en/function.empty.php