热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

从数组中获取特定值-fetchspecificvaluefromanarray

Ihavetheanarraycomingfromthiscode我有一个来自这段代码的数组echo<pre>;print_r($_SESSION[foll

I have the an array coming from this code

我有一个来自这段代码的数组

echo "
";
print_r($_SESSION["followers"]);
echo "
";

Array is

数组是

Array
(
    [pagination] => Array
        (
        )

    [meta] => Array
        (
            [code] => 200
        )

    [data] => Array
        (
            [0] => Array
                (
                    [username] => SD
                    [profile_picture] => https://instagramimages-a.akamaihd.net/profiles/profile_4.jpg
                    [id] => 42114932
                    [full_name] => A
                )

            [1] => Array
                (
                    [username] => ER
                    [profile_picture] => https://igcdn-photos-c-a.akamaihd.net/hphotos-ak-xpf1/t51.2885-19/1.jpg
                    [id] => 395834289
                    [full_name] => P
                )

        )

)

i want to know that is it possible to match specific value from array and then fetch other values related to it,

我想知道是否可以匹配数组中的特定值,然后获取与其相关的其他值,

e.g I am getting value of id = 42114932, now i wish to match this value in the given array and wherever it matches, i wish to fetch the username, fullname and profile pic of that id, in this case corresponding to id 42114932 i wish to get

例如,我正在获得id = 42114932的值,现在我希望在给定数组中匹配此值以及它匹配的任何地方,我希望获取该id的用户名,全名和个人资料照片,在这种情况下对应于id 42114932我希望要得到

username=SD, 
full_nmae=A
profile_pic = https://instagramimages-a.akamaihd.net/profiles/profile_4.jpg

if it is possible can anyone tell how it can be done

如果有可能,任何人都可以告诉它是如何做到的

5 个解决方案

#1


0  

An option is to loop trough the array

一个选项是循环通过数组

$f = ..;  //(the id to find)
for($i = 0, $i Username : '.$data[$i]['username'];
        echo '
Profile picture : '.$data[$i]['profile_pic']; } }

I think this could work? might want to tweak the counter of the for loop.

我觉得这可行吗?可能想要调整for循环的计数器。

#2


0  

function getMetaInfo(array $array, $id, $arrayBranch = NULL){ 
    if($arrayBranch === NULL){
        return FALSE;
    }

    $returnArray = array();

    foreach($array[$arrayBranch] as $key => $value){
         if($value['id'] == $id){
              unset($value['id']);
              $returnArray[$id] = $value;
              return $returnArray;
         }
    }

}

#3


0  

This code might help you :

此代码可能对您有所帮助:

  $arr = $_SESSION["followers"]['data'];
    $id = 42114932;
    $count = count($arr);
    $foundArr = array();
    for ($i = 0; $i<$count; $i++) {
       if ($arr['id'] == $id) {
         $foundArr[$i] = $arr[$i];
         break;
       }
    }
    print_r($foundArr)

#4


0  

The Code below is self explanatory:

以下守则是不言自明的:

";
print_r($_SESSION["followers"]);
echo "
"; $yourID = 42114932; for($x=0;$x<(count($_SESSION["followers"]['data']));$x++){ if($yourID == $_SESSION["followers"]['data'][$x]['id']){ //compare your id to each record echo "Full name : ".$array['data'][$x]['full_name']; echo "
Username : ".$array['data'][$x]['username']; echo "
Profile picture : ".$array['data'][$x]['profile_picture']; echo "
"; } }

#5


0  

You can try something like this:

你可以尝试这样的事情:

 $row = fetchData($_SESSION["followers"]['data'], 'id', 42114932); // Here you will get the comlet row of data you are searching

function fetchData($data, $column, $value){
    for($i = 0, $i 

推荐阅读
  • 如何使用Java获取服务器硬件信息和磁盘负载率
    本文介绍了使用Java编程语言获取服务器硬件信息和磁盘负载率的方法。首先在远程服务器上搭建一个支持服务端语言的HTTP服务,并获取服务器的磁盘信息,并将结果输出。然后在本地使用JS编写一个AJAX脚本,远程请求服务端的程序,得到结果并展示给用户。其中还介绍了如何提取硬盘序列号的方法。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 本文整理了315道Python基础题目及答案,帮助读者检验学习成果。文章介绍了学习Python的途径、Python与其他编程语言的对比、解释型和编译型编程语言的简述、Python解释器的种类和特点、位和字节的关系、以及至少5个PEP8规范。对于想要检验自己学习成果的读者,这些题目将是一个不错的选择。请注意,答案在视频中,本文不提供答案。 ... [详细]
  • 本文介绍了Foundation框架中一些常用的结构体和类,包括表示范围作用的NSRange结构体的创建方式,处理几何图形的数据类型NSPoint和NSSize,以及由点和大小复合而成的矩形数据类型NSRect。同时还介绍了创建这些数据类型的方法,以及字符串类NSString的使用方法。 ... [详细]
  • 1.移除consol.log()的babel插件安装:npmibabel-plugin-transform-remove-console-D配置:babel.config.js:这 ... [详细]
  • 小白的Python 学习笔记(八)推导式详解
    大家好,今天我总结一下Python的推导式,首先让我们来看定义推导式(comprehensions)是Python的一种独有特性,是可以从一个数据序列构建另一个新的数据序列的结构体 ... [详细]
  • AngularJS 提交表单的方式(一)
    英文原文:SubmittingAJAXForms:TheAngularJSWay在AngularJS出现之前,很多开发者就面对了表单提交这一问题。由于提 ... [详细]
  • |Questions|Answers|-------------|----------------------------------------- ... [详细]
  • 用JavaScript实现的太空人手表
    用JavaScript实现的太空人手表-JS写的太空人手表,没有用canvas、svg。主要用几个大的函数来动态显示时间、天气这些。天气的获取用到了AJAX请求。代码中有详细的注释 ... [详细]
  •    在用svn的时候,我们通常会使用下面的目录结构   projectname      -head      - ... [详细]
  • 数据库_常见问题
    1.phpMyAdmin安装时,连接数据库出错:错误信息如下:解决办法:1.将config.sample.inc.php改成config.inc.php2. ... [详细]
  • 展开全部下面的代码是创建一个立方体Thisexamplescreatesanddisplaysasimplebox.#Thefirstlineloadstheinit_disp ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • 文章目录题目:二叉搜索树中的两个节点被错误地交换。基本思想1:中序遍历题目:二叉搜索树中的两个节点被错误地交换。请在不改变其结构的情况下 ... [详细]
  • 准备工作参考我的另一篇利用githubactions自动部署gradle构建的springboot项目打包的war包到tomcat服务器,这里直接上配置, ... [详细]
author-avatar
初初初初丶初崽崽__冏每_472
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有